我正在尝试从Web服务器下载使用PDFsharp创建的PDF文件,并将其下载到本地PC的download文件夹。我在Google上搜索了很多,尝试了很多,但都没有成功。我可以正确创建PDF文件并将其保存到磁盘-但无法将其传输/下载到本地PC的下载文件夹。
我尝试了以下代码:
string filename = Server.MapPath("~\\Files\\Temp\\file.pdf");
document.Save(filename); //generate the physical file
MemoryStream stream = new MemoryStream();
document.Save(stream, false);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", stream.Length.ToString());
Response.BinaryWrite(stream.ToArray());
Response.Flush();
stream.Close();
Response.End();
所以我在做什么错呢?
缺少Content-Disposition
:
Response.AddHeader("Content-Disposition", "attachment;filename=test.pdf");
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Length", binfile.Length.ToString());
Response.BinaryWrite(binfile);