2009-10-23 129 views

回答

2

您需要創建一個URL來處理圖片並將DB內容返回到響應流中。正如發生在SQL Saturday #26我有一個演示文稿,顯示了這一點。您可以從鏈接doaloand我的幻燈片,進入演示2,並在lotsOfPictures解決方案,你會發現Picture.aspx.cs,已經做了你問什麼了:

using (SqlConnection conn = new SqlConnection(connStr)) 
      { 
       conn.Open(); 

       SqlCommand cmd = new SqlCommand(
@"SELECT picture 
FROM resized_pictures 
WHERE picture_id = @id 
AND picture_size = @size;", conn); 
       cmd.Parameters.AddWithValue("@id", pictureId); 
       cmd.Parameters.AddWithValue("@size", size); 

       using (SqlDataReader rdr = cmd.ExecuteReader(
        CommandBehavior.SequentialAccess)) 
       { 
        if (rdr.Read()) 
        { 
         Response.ContentType = "image/jpeg"; 
         byte[] bytes = new byte[1024]; 
         long offSet = 0; 
         int countRead = (int) rdr.GetBytes(
          0, offSet, bytes, 0, 1024); 
         while (countRead > 0) 
         { 
          Response.OutputStream.Write(bytes, 0, countRead); 
          offSet += countRead; 
          countRead = (int)rdr.GetBytes(
           0, offSet, bytes, 0, 1024); 
         } 
        } 
       } 
      } 

難題的重要作品是傳遞給SqlCommand閱讀器的SequentialAccess標誌將返回一個真正的流,因此頁面在返回之前不會在內存中加載整個圖像。對於高性能服務器,您應該使用異步操作,如Asynchronous Pages in ASP.NET 2.0中所述。

0

自己還沒有嘗試過,但是如何將數據流式傳輸到本地文件系統上的臨時文件,然後提供一個指向該臨時文件的鏈接?

0

通常情況下,您將使用您的服務器端代碼發送相應的headers,然後僅回顯內容。

從PHP手冊:

// We'll be outputting a PDF 
header('Content-type: application/pdf'); 

// It will be called downloaded.pdf 
header('Content-Disposition: attachment; filename="downloaded.pdf"'); 

// The PDF source is in original.pdf 
readfile('original.pdf'); 
0

如果你的文件存儲在數據庫中,你可能把它作爲一個博客或字節數組。在超鏈接點擊事件中,您需要將字節數組傳遞到流中,並使用流式寫入器來創建文件。然後執行該文件。

相關問題