【发布时间】:2019-06-28 12:29:51
【问题描述】:
所以我有一个使用此代码预览 PDF 文件的代码:
System.Net.WebClient client = new System.Net.WebClient();
//text box holds the path to the original pdf file in a folder
Byte[] Thisbuffer = client.DownloadData(TextBox.Text);
if (Thisbuffer!= null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", Thisbuffer.Length.ToString());
Response.BinaryWrite(Thisbuffer);
}
当我使用以下选项进行下载时,它可以在 chrome、IE 和 Edge 中正常查看 PDF 文件,但在 chrome 上查看:
它将文件下载为 .aspx 而不是 .pdf。这仅在 chrome 中发生,而不在 IE 或 Edge 中发生。
我已经尝试过Response.AddHeader("content-disposition", "attachment; filename=myPDFfile.pdf");,并且此自动下载文件不允许它在下载前查看。
任何帮助表示感谢谢谢!
【问题讨论】:
-
@Shanjee 感谢您的评论,但正如您在该示例中看到的那样,它要求在我的代码中明确指定的内容类型,我什至尝试使用 http 客户端它不起作用