[SOLVED] ASP.NET how to return pdf file as response without changing response header

Issue

I want to return a pdf file as response to some button click.
I succeeded to send the pdf file, but when i try to save it via the browser, it won’t let me save it as a .pdf file (but as .aspx file)

here’s the code:

Dim myWebClient As WebClient = New WebClient()
Dim myDataBuffer As Byte() = myWebClient.DownloadData(LocalImageURL) ' LocalImageURL is some path to a pdf file
Response.ContentType = "application/pdf"

Response.BinaryWrite(myDataBuffer)
Response.Flush()

Response.End()

if I am adding also the following line before writing the byte array:

Response.AddHeader("content-disposition", "attachment;filename=report.pdf")

it does the trick, but the problem is that the page remains stuck (looks like it still waits for server response to come)

Solution

This has been asked and answered before. Details are at the link below:

asp.net VB.net file download handler not work properly

Answered By – Prescott Chartier

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *