バイナリ列に格納されているSQLServerデータベースから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を使用すると、ページのコンテンツがファイルのコンテンツと一緒に送信されないようになると信じていたため、混乱しています。
私はここで何が間違っているのですか?
手伝ってくれてありがとう!