Click to See Complete Forum and Search --> : ThreadAbortException - file download


THY02K
October 25th, 2005, 11:18 PM
Hi, I'm writing this small code segment which handles file download:


Response.Clear()
Response.ContentType = mime_type Response.OutputStream.Write(item, 0, item.Length)
Response.End()


However, ThreadAbortException is raised everytime Response.End is called:

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort(Object stateInfo)
at System.Web.HttpResponse.End()
at

However, this seems to be okay on my development box: ie. Client was able to download the file nonetheless. However, when this same app is deployed to Production machine, ASP.NET on production machine seems to be handling ThreadAbortException differently - the exception was still raised, I caught it with a try-catch block, users are still able to download the file (PDF btw), but download time is like 10 minutes!? (While same app on development download is like 5 sec - same PDF file) - it seems like production and development box is handling ThreadAbortException differently.

I tried to get around this by Response.Flush instead of Response.End but then a different (client side) exception arise saying the file is corrupted ("File is damaged and could not be repaired")??!

Just before I go... Response.Redirect("...", False) won't do - I'm trying to do a download here. (ie. I write to response stream ... a Save File dialog pops on client side)

mmetzger
October 25th, 2005, 11:33 PM
What happens if you don't put Response.End() in place, or possibly wait a time period before doing so?

According to all the docs I can find, Response.End is sort of a bad idea as it stops all processing of ASP.NET on that thread (and hence your thread error.)

THY02K
October 26th, 2005, 04:33 AM
Thanks but here's my observation:

OPTION 1. Response.End


Response.OutputStream.Write(item, 0, item.Length)
Response.End()


On development box, I could download the file no problem (pdf is loaded inside the popup and user is able to view within popup/save it to disk with Acrobat Reader's toolbar). When I deploy the application to production machine, this seems to slow down the dowload a lot (to ten minutes, not acceptable)

OPTION 2. Response.Flush() - in both (2.2) and (2.3) file seems corrupted.

OPTION 2.1
Response.OutputStream.Flush()

Result is, no exception is registered. But the popup in which the pdf is supposed to display goes blank + not responsive. (Not sure if I wait ten minutes if the thing will load but it's a very small pdf)

OPTION 2.2

Response.OutputStream.Flush()
Response.OutputStream.Close()


Result is, immediately on client side, a popup saying "File is damaged and could not be repaired"

OPTION 2.3

Response.OutputStream.Flush()
Response.OutputStream.Close()
Context.Current.ApplicationInstance.CompleteRequest()


Same result as (2.2)

I was expect that it could be a bug with Acrobat Reader/IIS (Reference: http://www.kbalertz.com/kb_316864.aspx) -
(1) I checked and confirmed that my IIS is patched
AND
(2) RemoveServerHeader=0 in urlscan's "ini" file

It did not help.

THY02K
October 26th, 2005, 10:50 PM
okay, I have attached a few pictures .... of the popup I created to do file upload/download, here's the code segment responsible for download


Response.Clear()
Response.ContentType = mime_type
Response.OutputStream.Write(item, 0, item.Length)
Response.OutputStream.Flush()
Response.OutputStream.Close()
'Context.Current.ApplicationInstance.CompleteRequest()

'.Response.End()


1. Another question I have is, sometimes when you download you get "File Download" dialog. Other times the document is viewed "in-place" inside the popup... how do you control this behavior?

2. How can you specify "file name" -- it seems like file name in "File Download" dialog becomes "SomePage.aspx"??