简体   繁体   English

从SQL Server读取Blob并下载

[英]Reading blob from SQL Server and downloading it

I am updating an asp.net application and saving and retrieving documents from a mssql database. 我正在更新asp.net应用程序,并从mssql数据库保存和检索文档。 Saving was very simple but is there a way to download a file from an anchor that directly downloads from the database? 保存非常简单,但是有没有办法从直接从数据库下载的锚中下载文件? Lets just say that that blob is bound within a gridview or some other data control, I would like to do this, example: <a href="<%#Eval("blobcolumn")%">Download Doc</a> . 可以说,blob绑定在gridview或其他数据控件中,我想这样做,例如: <a href="<%#Eval("blobcolumn")%">Download Doc</a> I save the file name as well. 我也保存文件名。

First, you wouldn't assign the image to the href element, you would create an img tag to display the image, then the href tag to download it or embed the image tag directly in the href: 首先,您不会将图像分配给href元素,而是创建img标签以显示图像,然后创建href标签以下载图像或将图像标签直接嵌入href中:

<a href="http://somelink.com"><img src="<%#Eval("blobcolumn")%></img></a>

Second, displaying the image inline without Url to a file requires a Data URI , which means that a base-64 version of the image is embedded in the web page: 其次,不带URL向文件内联显示图像需要一个Data URI ,这意味着该图像的base-64版本已嵌入到网页中:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

This approach means a substantial amount of extra traffic on your web server and your end user's machines since the image will not be cached as it would be if you provided an actual URL to the image in your web server (ie /images/reddot.png). 这种方法意味着您的Web服务器和最终用户机器上的大量额外流量,因为该图像将不会被缓存,就像您在Web服务器中为该图像提供实际URL一样(即/images/reddot.png) )。

I think that you would be much better off saving copies of the image to the web server and providing urls to those images in your page. 我认为将图像的副本保存到Web服务器并在页面中提供这些图像的URL会更好。

If there is concern about the size or number of files, you could create batch jobs to purge files older than a certain number of days or hours from the temporary image cache. 如果担心文件的大小或数量,则可以创建批处理作业,以从临时映像缓存中清除超过特定天数或小时数的文件。

You can create a Handler to download data. 您可以创建一个处理程序来下载数据。

sample.ashx sample.ashx


public void ProcessRequest (HttpContext context) 
{
  string id = context.Request["id"];
  if (!string.IsNullOrEmpty(id))
     {
       //read byte[] and other data from database based upon given ID
       byte []ar=//result from database
       string fileName=//filename 
       context.Response.ContentType = "Application/octet-stream";
       context.Response.AddHeader("Content-Length", ar.Length.ToString());
       context.Response.AddHeader("Content-Disposition", "attachment; filename=" +  fileName );
       context.Response.BinaryWrite(ar);
       context.Response.Flush();
       context.Response.End();
      }
}

and request via hyperlinks 并通过超链接请求

<a href="sample.ashx?id=101">Download .zip</a>
<a href='sample.ashx?id=<%#Eval("ID")%>'><%#Eval("Desc")%></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM