asp.net fileUpload不会两次上传相同的文件

时间:2019-07-22 18:05:54

标签: c# asp.net .net file-upload webforms

我已经检查了几个解决此问题的方法,当您第一次上载a.jpg很好,但是当您再次上载a.jpg时,它将不起作用。 再次上传a.jpg的唯一方法是上传b.jpg。

我的代码如下

 <p>Select file to upload:</p>
    <asp:FileUpload ID="FileUploader" runat="server" Width="1000px" />
 <br />

服务器代码如下所示

protected void FileUploadButton_Click(object sender, EventArgs e)
    {
        try
        {
            //File upload logic. Returns path of uploaded file
            string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUploader.PostedFile.FileName);

            //File save to server. Saves file name as uploaded by user to folder, "Files" on the server
            string path = System.IO.Path.Combine("~/Files/",Path.GetFileName(FileUploader.PostedFile.FileName));

            FileUploader.SaveAs(Server.MapPath(path));

            //Function to insert values in excel sheet to database
            InsertIntoDatabase(filePath)
        }
        catch (Exception Ex)
        {
        }//End try

    }//End FileUpload 

我已阅读解决方案,将fileUploader放在更新面板上。上传后,我也尝试过重命名文件。那行得通,但是却破坏了我的逻辑

1 个答案:

答案 0 :(得分:0)

多亏了pcalkins,我重新保存了文件,然后服务器认为它不能处理同一文件

  protected void FileUploadButton_Click(object sender, EventArgs e)
    {
        try
        {
            //File upload logic. Returns path of uploaded file
            string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUploader.PostedFile.FileName);

            //File save to server. Saves file name as uploaded by user to folder, "Files" on the server
            string path = System.IO.Path.Combine("~/Files/",Path.GetFileName(FileUploader.PostedFile.FileName));

            string day = DateTime.Now.ToString("ss_mm_hh_dd_MM_yyyy");

            FileUploader.SaveAs(Server.MapPath(path));

            //Function to insert values in excel sheet to database
            InsertIntoDatabase(filePath)

             //Resave file to keep track of uploaded files
            File.Copy(filePath, day + filePath);
            File.Delete(filePath);
        }
        catch (Exception Ex)
        {

        }//End try

    }//End FileUpload