deleting pdf files RRS feed

  • Question

  • User-1651858287 posted

    I have a folder which requires a password to access for example /admin/userpage.aspx.

    However, the user creates a pdf is created in the folder called /admin/username.pdf

    I want to find a way to delete this file (/admin/username.pdf) preferable when the user closes the browser

    /admin/userpage.aspx.  or at midnight for or any other way.



    Monday, September 2, 2019 10:44 PM

All replies

  • User288213138 posted

    Hi sweetSteal,

    Normally, deleting a file is deleted from the server, regardless of the local password(Unless you have special Settings).

    I made demo for you about how to delete a file when close the browser.

    I used onbeforeunload() function to detect when the browser closes, When it is closed, the delete event is executed.

    The code:

    <body onbeforeunload="quit();">
        <form id="form1" runat="server">
                <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>       
            <script type="text/javascript">
                function quit() {
                    event.returnValue = "Are you sure you want to leave?"
                    document.getElementById('<%= btnDelete.ClientID %>').click();
    protected void btnDelete_Click(object sender, EventArgs e)
            private void DeleteDirectory()
                string rootFolder = @"C:\Users\samwu\Desktop\admin\";  
                string authorsFile = "username.pdf";
                    if (File.Exists(Path.Combine(rootFolder, authorsFile)))
                        File.Delete(Path.Combine(rootFolder, authorsFile));
                        Response.Write("File deleted.");
                        Response.Write("File not found");
                catch (IOException ioExp)

    The result:

    About how to delete file at midnight, you can refer to this link: How to Schedule Programs to Run Automatically 

    You can use Schedule to automate programs.

    Best regards,


    Tuesday, September 3, 2019 6:49 AM
  • User-1651858287 posted

    Thanks for the help but this does not work for me.

    I do not a delete button needs to it automatically without the user doing anything or knowing about.\

    The onbeforeunload()  does not work for me either chrome just closes without executing the code.


    Monday, September 9, 2019 2:37 PM
  • User475983607 posted

    Simply it's not possible in modern browsers.

    Monday, September 9, 2019 3:04 PM