I wonder if anyone knows this one.
I was getting a pretty tough error on my asp.net vb web app. I tell you I nearly had qwerty emblazened on my forehead for bangin my head against the keyboard. But I think I figured out the cause of the error:
I have the following code:
fileToDownload = createFileSet(fileToDownload, confirm)
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileToDownload.Name)
HttpContext.Current.Response.AddHeader("Content-Length", fileToDownload.Length.ToString())
HttpContext.Current.Response.ContentType = ReturnExtension(fileToDownload.Extension.ToLower())
HttpContext.Current.Response.TransmitFile(fileToDownload.FullName)
HttpContext.Current.ApplicationInstance.CompleteRequest()
Directory.Delete("C:\temp_" & confirm, True)
'System.IO.File.Delete(fileToDownload.FullName) <--- Error here
When I take the comment tick off that last line I get a real ugly error (I could redo it and spell it out, what kind of error, but it really doesn’t matter).
I think I know the cause. The user is downloading a file, or in process of, or something, and the program is attempting to delete it… causing the error.
Is there a way of creating some on HttpContext.Current.Response.TransmitFile.complete (code made up) or am I resorted to a batch file and a windows scheduler to clean up these files that are no longer needed?
Is my error conclusion correct, you think? I really don’t see a way of creating an “on event” but I’m too new at asp.net to know for sure.
Try using a finally block (got the idea from here http://forums.asp.net/t/1436818.aspx/1 ). Inshort do this…
Or do this:
You could read the zip file contents into a memory stream, delete the file then push the memory stream out to the response buffer. Only issue I can see with that is if the download fails and they want to try and get it again. Anyway heres a post showing you how to deal with files and memory streams and pushing it out to the response buffer http://www.c-sharpcorner.com/UploadFile/jhblankenship/DownloadingFromMemStream11262005060834AM/DownloadingFromMemStream.aspx