为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
莫回无 2023-07-09 17:47:58
我检查了这个问题的几种解决方案,当你第一次上传 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 回答

?
慕田峪7331174

TA贡献1828条经验 获得超13个赞

我重新保存了文件,然后服务器认为它不处理相同的文件


  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 


查看完整回答
反对 回复 2023-07-09
  • 1 回答
  • 0 关注
  • 111 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信