问题描述
我有一个 VARBINARY(最大)
列,基本上是一个SQL Server表COM pressed数据。
I have a SQL Server table with a Varbinary(Max)
column that is basically compressed data.
我的页面,用户可以下载此数据(通常用户身份验证后)。
My page allows users to download this data (after usual user authentication).
它曾经工作确定,较小的数据大小,但现在随着时间的推移,数据也越来越大。我现在面临很多的问题基本上是一个等待时间,出现保存对话框之前。
It used to work ok, with smaller data size, but now with time, the data is also getting bigger. I am facing lot of problems basically a wait time, before the Save dialog appears.
code:
while (reader.Read())
{
Response.Buffer = false;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/gzip";
Response.AddHeader("content-disposition", "attachment;filename="
+ "vbet_1_1.sdf.gz");
byte[] bytes = (Byte[])reader["backupdata"]; // STUCK HERE
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
在调试器,我可以看到,
In the debugger, I can see that
byte[] bytes = (Byte[])reader["backupdata"];
在这里的滞后。
我的平台是ASP.Net与.NET框架4.0,SQL Server 2008中,C#codebehind
My platform is ASP.Net with .NET Framework 4.0, SQL Server 2008, C# codebehind
推荐答案
您需要流响应返回。读取内存中的所有文件,然后写出来是不会扩展,你会开始exhasting服务器的请求数或的文件的大小增加。
You need to stream the response back. Reading the entire file in memory and then writing it out is not going to scale and you'll start exhasting the server as the number of requests or the size of the files increase.
看一看的的下载和上传图像,看看如何做到这一点的例子。当你不使用MVC,但直ASP.NEt你可以做更简单,但想法是一样的:
Have a look at Download and Upload images from SQL Server via ASP.Net MVC and FILESTREAM MVC: Download and Upload images from SQL Server to see an example of how to do this. As you do not use MVC but straight ASP.NEt you can do it simpler, but the ideas are the same:
- 使用一个流API来读取数据库的响应(特别是
SqlCommand.ExecuteReade
与)和读取使用的(注意的重要性在MSDN上再次小文档片断SequentialAccess
标志...) - 使用流API写的HTTP响应。 就行了。
- use a streaming API to read the database response (specifically
SqlCommand.ExecuteReade
withCommandBehavior.SequentialAccess
) and read the response in chunks, usingSqlDataReader.GetBytes()
(note again the small snipped on MSDN about the importance ofSequentialAccess
flag...) - use a streaming API to write the HTTP response.
Response.BinaryWrite()
will do.
这篇关于下载从SQL Server varbinary数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!