Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to delete a file after it's opened in a browser?

How to delete a file after it's opened in a browser?

Scheduled Pinned Locked Moved ASP.NET
questionhelptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JimFeng
    wrote on last edited by
    #1

    I need to delete a PDF file after it’s opened in browser. The following code give me “Page can not be found error”. How can I make sure that the file has been open in browser before deleting it? If File.Exists(fileName) = True Then Dim sScript As New System.Text.StringBuilder sScript.Append("" & vbCrLf) sScript.Append("Report = window.open('" & fileName & "','Report', '');" & vbCrLf) sScript.Append("if (parseInt(navigator.appVersion) >= 4) Report.window.focus();" & vbCrLf) sScript.Append("" & vbCrLf) Page.ClientScript.RegisterStartupScript(GetType(Page), "winReport", sScript.ToString) page.Response.Clear() File.Delete(fileName) End If

    P C 2 Replies Last reply
    0
    • J JimFeng

      I need to delete a PDF file after it’s opened in browser. The following code give me “Page can not be found error”. How can I make sure that the file has been open in browser before deleting it? If File.Exists(fileName) = True Then Dim sScript As New System.Text.StringBuilder sScript.Append("" & vbCrLf) sScript.Append("Report = window.open('" & fileName & "','Report', '');" & vbCrLf) sScript.Append("if (parseInt(navigator.appVersion) >= 4) Report.window.focus();" & vbCrLf) sScript.Append("" & vbCrLf) Page.ClientScript.RegisterStartupScript(GetType(Page), "winReport", sScript.ToString) page.Response.Clear() File.Delete(fileName) End If

      P Offline
      P Offline
      Psycho Coder Extreme
      wrote on last edited by
      #2

      I could be wrong here but I think if you attempt to delete a file thats currently open you're going to get an error as you cannot delete somethings that opened (being used)

      "Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch

      J 1 Reply Last reply
      0
      • P Psycho Coder Extreme

        I could be wrong here but I think if you attempt to delete a file thats currently open you're going to get an error as you cannot delete somethings that opened (being used)

        "Well yes, it is an Integer, but it's a metrosexual Integer. For all we know, under all that hair gel it could be a Boolean." Tom Welch

        J Offline
        J Offline
        JimFeng
        wrote on last edited by
        #3

        The error message “Page can not be found” indicates that the file has been deleted when browser try to open the file. When I comment out the code File.Delete(fileName) the new browser window will open the file. And also I can manually delete the file without any error when the browser still open.

        1 Reply Last reply
        0
        • J JimFeng

          I need to delete a PDF file after it’s opened in browser. The following code give me “Page can not be found error”. How can I make sure that the file has been open in browser before deleting it? If File.Exists(fileName) = True Then Dim sScript As New System.Text.StringBuilder sScript.Append("" & vbCrLf) sScript.Append("Report = window.open('" & fileName & "','Report', '');" & vbCrLf) sScript.Append("if (parseInt(navigator.appVersion) >= 4) Report.window.focus();" & vbCrLf) sScript.Append("" & vbCrLf) Page.ClientScript.RegisterStartupScript(GetType(Page), "winReport", sScript.ToString) page.Response.Clear() File.Delete(fileName) End If

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          the core question is, what are you trying to do ? You really cannot tell when this code has finished reading the file, perhaps if it's a temporary report, your code should delete then in batches. If you're using the same filename over and over, then you risk collisions if you have more than one user, so I assume you're creating unique filenames. Perhaps a service that deletes files that are more than an hour old ? The other way is to create a popup that calls an aspx, which reads the PDF and pushes it down in the code behind, setting the mime type, etc. That way, you'll know it's gone and can delete it.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          J 1 Reply Last reply
          0
          • C Christian Graus

            the core question is, what are you trying to do ? You really cannot tell when this code has finished reading the file, perhaps if it's a temporary report, your code should delete then in batches. If you're using the same filename over and over, then you risk collisions if you have more than one user, so I assume you're creating unique filenames. Perhaps a service that deletes files that are more than an hour old ? The other way is to create a popup that calls an aspx, which reads the PDF and pushes it down in the code behind, setting the mime type, etc. That way, you'll know it's gone and can delete it.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            J Offline
            J Offline
            JimFeng
            wrote on last edited by
            #5

            What I'm trying to do is to create a PDF file from a local report viewer and open the file from IE browser. In this way, client have full control on the report, like to print selected page. (I know that the server side report viewer can do the same but we're not ready to use it yet.) I use session ID as file name so it should no problem for other session user. Can you show me some examples on popup and mime type? Thanks in advance.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups