Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用新上载文件替换旧文件_C#_Asp.net_File_File Upload - Fatal编程技术网

C# 用新上载文件替换旧文件

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

我正在创建一个web应用程序,其中我必须用
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));
    }
}

谢谢你的回复。在你的情况下,它只会删除相同的文件。我想用新文件删除现有文件。无论如何,谢谢你的回复。谢谢你的回复。在你的情况下,它只会删除相同的文件。我想用新文件删除现有文件。无论如何,谢谢你的回复。。