2009-10-23 135 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

如果你的文件存储在数据库中,你可能把它作为一个博客或字节数组。在超链接点击事件中,您需要将字节数组传递到流中,并使用流式写入器来创建文件。然后执行该文件。

相关问题