I have a generic handler that serves files for download:
Dim request As HttpRequest = context.Request
Dim response As HttpResponse = context.Response
response.ContentType = "application/octet-stream"
response.AddHeader("content-disposition", "inline; filename=" & filename)
response.Buffer = True
response.OutputStream.Write(fileBytes, 0, fileBytes.Length)
response.Flush()
response.Close()
('fileBites' is my bite array, 'filename' is my file name).
When fileBites is, say, a .txt file - the download is triggered and the file is read perfectly.
I discovered, however, that .pdf and .docx files were being corrupted - In the case of .docx, Word was saying that the file needed to be recovered and asked me for permission to do so. When I granted this permission it fixed it immediately and displayed perfectly.
Obviously I didn't want users to see this corruption dialogue and after researching for a while I discovered this: http://forums.asp.net/t/1301978.aspx/1/10 - which suggested that the reason for the corruption was one extra empty bit was being written at the end of the byte array: I checked by dropping the length by one bit:
response.OutputStream.Write(fileBytes, 0, fileBytes.Length - 1)
and like magic, .docx downloads now work! (This is not my current problem, I include it for context and in case anybody else has the same issue)
My current problem is that although .docx files are now streaming correctly, .pdf files are not. They seem to transfer in one piece (at the correct KB size) but when I try and open the downloaded file Adobe Reader X tells me:
Adobe Reader could not open xxxx because it is either not a supported file type
or because the file has been damaged (for example, it was sent as an email
attachment and wasn't correctly decoded).
There was a fairly long unresolved discussion on the adobe forums dated 2008 (http://forums.adobe.com/thread/391712) that addresses this exact issue but this is now dead. I have tried all of the workarounds that users have posted (content type: /pdf not /octet, disposition: application not inline, different content-encodings and charsets, etc) but all to no avail.
I wonder if anybody has encountered this problem before that could point me somewhere vaguely approximating something that even remotely resembles the right direction!