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;
}
}
}