我想从存储在二进制列中的 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 会确保页面内容不会与文件内容一起发送。
我在这里做错了什么?
感谢帮助!