我想从存储在二进制列中的SQL Server数据库下载PDF文件。在aspx页面上有一个LinkButton。此按钮的事件处理程序如下所示:
protected void LinkButtonDownload(object sender, EventArgs e)
{
...
byte[] aByteArray;
// Read binary data from database into this ByteArray
// aByteArray has the size: 55406 byte
Response.ClearHeaders();
Response.ClearContent();
Response.BufferOutput = true;
Response.AddHeader("Content-Disposition", "attachment; filename=" + "12345.pdf");
Response.ContentType = "application/pdf";
using (BinaryWriter aWriter = new BinaryWriter(Response.OutputStream))
{
aWriter.Write(aByteArray, 0, aByteArray.Length);
}
}
我的浏览器中提供了“文件打开/保存对话框”。当我将此文件“12345.pdf”存储到磁盘时,该文件的大小为71523字节。 PDF文件末尾的额外16kB是我页面的HTML代码(正如我在编辑器中查看文件时所看到的)。我很困惑,因为我相信ClearContent和ClearHeaders会确保页面内容不会与文件内容一起发送。
我在这里做错了什么?
感谢您的帮助!
答案 0 :(得分:3)
我认为你想在这个方法结束时使用Response.End。
答案 1 :(得分:3)
快速浏览一下,你就错过了Response.End();