C# 用新上载文件替换旧文件
我正在创建一个web应用程序,其中我必须用C# 用新上载文件替换旧文件,c#,asp.net,file,file-upload,C#,Asp.net,File,File Upload,我正在创建一个web应用程序,其中我必须用fileupload上传的新文件替换已上传的文件 我正在使用以下代码: void UploadFile() { HttpPostedFile PostedFile = Request.Files["FileUploadExcel"]; if (PostedFile != null && PostedFile.ContentLength > 0) { M
fileupload
上传的新文件替换已上传的文件
我正在使用以下代码:
void UploadFile()
{
HttpPostedFile PostedFile = Request.Files["FileUploadExcel"];
if (PostedFile != null && PostedFile.ContentLength > 0)
{
MyFile = Path.GetFileName(PostedFile.FileName);
PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));
Get_Data(MyFile);
}
else
{
LblMessage.Text = "Missing File";
LblMessage.Visible = true;
}
}
请更新代码以用新上载的文件替换现有文件。只需添加即可
File.Delete(Server.MapPath(Path.Combine("~/Data/", MyFile)));
在调用SaveAs之前。只需添加
File.Delete(Server.MapPath(Path.Combine("~/Data/", MyFile)));
在您的SaveAs呼叫之前。试试这个
//determine if file exist
If(File.Exists(Server.MapPath(Path.Combine("~/Data/", MyFile))))
{
//delete existing file
File.Delete(Server.MapPath(Path.Combine("~/Data/", MyFile)));
}
PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));
试试这个
//determine if file exist
If(File.Exists(Server.MapPath(Path.Combine("~/Data/", MyFile))))
{
//delete existing file
File.Delete(Server.MapPath(Path.Combine("~/Data/", MyFile)));
}
PostedFile.SaveAs(Server.MapPath(Path.Combine("~/Data/", MyFile)));
试试这个:
if (FLUpload.PostedFile != null && FLUpload.PostedFile.FileName != "")
{
if (System.IO.Directory.Exists(Server.MapPath("~/Files/")) == false)
{
System.IO.Directory.CreateDirectory(Server.MapPath("~/Files/"));
System.IO.Directory.Delete(Server.MapPath("~/Files/") + path);
}
else
{
FLUpload.SaveAs(Server.MapPath(path));
}
}
试试这个:
if (FLUpload.PostedFile != null && FLUpload.PostedFile.FileName != "")
{
if (System.IO.Directory.Exists(Server.MapPath("~/Files/")) == false)
{
System.IO.Directory.CreateDirectory(Server.MapPath("~/Files/"));
System.IO.Directory.Delete(Server.MapPath("~/Files/") + path);
}
else
{
FLUpload.SaveAs(Server.MapPath(path));
}
}
谢谢你的回复。在你的情况下,它只会删除相同的文件。我想用新文件删除现有文件。无论如何,谢谢你的回复。谢谢你的回复。在你的情况下,它只会删除相同的文件。我想用新文件删除现有文件。无论如何,谢谢你的回复。。