2010-03-17 25 views
0

我想從存儲在二進制列中的SQL Server數據庫下載PDF文件。在aspx頁面上有一個LinkBut​​ton。這個按鈕的事件處理程序是這樣的:下載文件時,我在做什麼錯誤的HttpResponse內容和標題?

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會確保頁面內容不會與文件內容一起發送。

我在這裏做錯了什麼?

感謝您的幫助!

回答

3

我想你想要一個Response.End在這個方法的最後。

+0

太好了,謝謝,這很容易解決!我知道這必須是一個初學者的錯誤。 (你已經打了Ted一分鐘。) – Slauma 2010-03-17 20:32:00

3

快速瀏覽一下,你就會錯過Response.End();

相關問題