Reputation: 28403
The problem is that all supported files works properly (jpg, gif, png, pdf, doc, etc), but .docx files, when i download, it says corrupted and they need to be fixed by Office in order to be opened.
This is my code:
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
Response.BinaryWrite(RptBytes);
stream.Dispose();
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
I have got solution from some other site,
i.e
It turns out that the docx format needs to have Response.End() right after the Response.BinaryWrite instead of HttpContext.Current.ApplicationInstance.CompleteRequest()
Why are .docx files being corrupted when downloading from an ASP.NET page?
It's working fine but Response.End throws the exception as Thread was being aborted.
Upvotes: 3
Views: 2828
Reputation: 7735
Copying my comment to an answer:
My guess is all of your downloads are corrupted, except some file formats are more forgiving than others. An errant byte on a jpg or gif is probably not going to be noticed by the software displaying the image. However, docx files are actually zip files, which are more strict about corrupted bytes.
What I would do is:
then write the byte array to the Response
via BinaryWrite
// read the file to a byte array byte[] RptBytes = File.ReadAllBytes(pathToFile);
Then follow the code in https://stackoverflow.com/a/15869303/740639
I think your problem is how you are dealing with Response
. You could probably just follow the code in that answer and skip the whole byte array part.
Upvotes: 3