0

I have checked several solutions to this problem where when you upload a.jpg the first time is fine but when you upload a.jpg again, it won't work. The only way you could upload a.jpg again is when you upload b.jpg.

My code looks like this

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

And the server code looks like this

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 

I have read solutions where you put the fileUploader on an update panel. I also tried renaming the file after it is uploaded. That worked but it breaks my logic down the line

5
  • Seems like you'd also want to branch this to an "update" type function... (if file exists, present user with an "update" button instead of "upload"... and call a separate function.) Then you'd either delete the file first, or just update the stored path to the "a(1).jpg" name. (I'm assuming the file is uploaded, but the name is changed to avoid over-writing...) IMO you probably shouldn't delete the file. You never know when someone might want to recover that. Commented Jul 22, 2019 at 18:54
  • @pcalkins Thanks a lot. I have thought about deleting the file. Like you said they might want to recover that. Also, the insertion breaks when the file is renamed. That would have solved my problem but unfortunately, my insertion logic won't run unless the uploaded file matches the file you pass into it (Due to requirements BTW) Commented Jul 22, 2019 at 19:04
  • OK, so maybe you should rename the existing file first. Append the name with current datetime? Commented Jul 22, 2019 at 19:35
  • @pcalkins I tried that before but I quite didn't do it as you comment. Thanks it works now. Can you post your solution so I can accept? Commented Jul 22, 2019 at 20:35
  • better if you post your updated code... you can answer your own question(s). Commented Jul 22, 2019 at 20:39

1 Answer 1

1

Thanks to pcalkins, I resave the file and then the server doesn't think its handling the same file

  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 
Sign up to request clarification or add additional context in comments.

1 Comment

@pcalkins Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.