Reputation: 3
I'm developing a vb.net web application. Recently I face a problem where the PDF file with version 1.5 and higher are not able to view/download through browser. When I click the link to view the PDF file, the browser will prompt a "File Download" message box, when I choose to open the PDF file, the adobe reader display this error "There was an error opening this document. The file is damaged and could not be repaired." Even if I choose "Save" to PC then open the PDF file, it still display the same error message.
The code I'm using for view attachment is as below:
HTTPContext.Current.Response.ContentType = "APPLICATION/PDF"
HTTPContext.Current.Response.AppendHeader("Content-Disposition", "Attachment; Filename=XXX.pdf")
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.WriteFile(System.IO.FileInfo(FILE_PATH).FullName)
HttpContext.Current.ApplicationInstance.CompleteRequest()
There is no issue when viewing the PDF version 1.4 and below. Is there any issue with the code? Any solution to resolve this issue?
Upvotes: 0
Views: 748
Reputation: 1707
This should work for any file type:
Response.ContentType = "APPLICATION/OCTET-STREAM"
Dim Header As [String] = "Attachment; Filename=MyFile.PDF"
Response.AppendHeader("Content-Disposition", Header)
Response.BinaryWrite(MyFile.pdf)
Response.[End]()
Adjust accordingly.
NOTE: There are issues with Adobe Reader in protected mode which may be solved with a settings change. Check this link: https://forums.adobe.com/thread/2208915
Upvotes: 0