How to delete a file after it's opened in a browser?
-
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
-
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
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
-
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
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.
-
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
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 )
-
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 )
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.