0

Code Description: After uploading a file, the session is added to the code of another button and the uploaded file(pd) is used every time the button is pressed.

What I want: When the person, who uploaded the file, left the site after pressing the button as many times as he/she wanted, uploaded file need to be deleted from the upload folder.

I tried this as a solution: I writed a line of code to clear the folder whenever the upload button was pressed, but I don't think this is correct.

My question:This problem confused me, I can't think logically, please suggest a logical solution please

Note: (I simplified the code here, if there are other errors, it should be ignored)

  protected void btnUpload_Click(object sender, EventArgs e)
    {
        Array.ForEach(Directory.GetFiles(Server.MapPath("/upload/"), "*.pdf"), File.Delete); //

        int temptpage = 0;

            if (dlg.HasFile)
            {
                string text = System.IO.Path.GetExtension(dlg.FileName);
               

                if (text != ".pdf")
                {
                    lblUpload.Text = "Invalid file type";
                }
               else
                {
                       string path = Server.MapPath("/upload/");
                        dlg.SaveAs(path + dlg.FileName);
                        lblUpload.Text = "Uploaded";
                        string pd = path + dlg.FileName;
                        Session["SB_1"] = pd;
                }
           }
}
Aycan Candar
  • 63
  • 1
  • 9
  • General question: Are you saving the file on the webserver? How big are these files? Are these eventually going to a file server or database? – Sudip Shrestha Jul 13 '20 at 15:32
  • Put the files in a directory that has a subdirectory of session id. On end session, clear that folder. (just an idea) Right now, you'll empty it for everybody. – Nikki9696 Jul 13 '20 at 15:36
  • I wrote the condition of being less than 6mb, I simplified the code here. Since the files will be used instantly, I did not intend to use a database and bought a server without a database, but I am still testing the code on vstudio because there are problems with the server. Did I make a mistake not to take a database? : / @SudipShrestha – Aycan Candar Jul 13 '20 at 15:38
  • I use session only to be able to use one variable anywhere. I have no idea about other uses, but I will search. @Nikki9696 – Aycan Candar Jul 13 '20 at 15:41
  • @nasya There are pros and cons to such an approach, as with any, but here's a little example. I assume you don't have a way to schedule cleanup jobs and that would be overkill for what you need. https://stackoverflow.com/questions/24084588/delete-temp-files-on-session-end-in-asp-net – Nikki9696 Jul 13 '20 at 15:46

0 Answers0