ASP.NET 如何在不更改响应头的情况下 return 将 pdf 文件作为响应

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

我想 return 一个 pdf 文件作为对某些按钮点击的响应。 我成功发送了 pdf 文件,但是当我尝试通过浏览器保存它时,它不会让我将它保存为 .pdf 文件(而是保存为 .aspx 文件)

代码如下:

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()

如果我在写入字节数组之前还添加了以下行:

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

它成功了,但问题是页面仍然卡住(看起来它仍在等待服务器响应)

这个问题之前已经被问过和回答过。详情在下面的 link: