So i want to delete some files when a user closes the browser.
I’m using the page_unload method shown below:
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
string[] uploadedFiles = Directory.GetFiles(Server.MapPath("~/testPdfIn"));
foreach (string uploaded in uploadedFiles)
{
File.Delete(uploaded);
}
}
but the page_unload method will just fire right after the page load, deleting the files in folder without the user even closing the browser.
Is there a way to stop the page_unload from firing right after loading and only firing when user closes the browser?
It’s better you detect the browser close event and than call ther server side function using ajax will do for you