猿问

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

我检查了这个问题的几种解决方案,当你第一次上传 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 放在更新面板上的解决方案。我还尝试在上传文件后重命名该文件。这有效,但它破坏了我的逻辑


莫回无
浏览 112回答 1
1回答

慕田峪7331174

我重新保存了文件,然后服务器认为它不处理相同的文件&nbsp; protected void FileUploadButton_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //File upload logic. Returns path of uploaded file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string filePath = Server.MapPath("~/Files/") + Path.GetFileName(FileUploader.PostedFile.FileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //File save to server. Saves file name as uploaded by user to folder, "Files" on the server&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string path = System.IO.Path.Combine("~/Files/",Path.GetFileName(FileUploader.PostedFile.FileName));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string day = DateTime.Now.ToString("ss_mm_hh_dd_MM_yyyy");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileUploader.SaveAs(Server.MapPath(path));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Function to insert values in excel sheet to database&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InsertIntoDatabase(filePath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Resave file to keep track of uploaded files&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.Copy(filePath, day + filePath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File.Delete(filePath);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception Ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }//End try&nbsp; &nbsp; }//End FileUpload&nbsp;
随时随地看视频慕课网APP
我要回答