English 中文(简体)
Streaming uploaded files directly into a database table
原标题:

I have several applications that allow users to upload attachments that are stored inside a database table. This has worked fine for several years because it was initially intended for smallish image files, but now they want to upload very large files (~80MB). This is causing the server to run out of memory (and the upload to fail) because, in order to write the binary data to a database, I am loading the entire content into a byte array. Many, if not most, online examples for file uploads use this method (see http://www.aspnettutorials.com/tutorials/database/Save-Img-ToDB-Csharp.aspx as an example).

Now, the question is, can I somehow stream the binary content to the database instead of loading the entire content into a byte array and then setting the byte array as a parameterized value? Switching to file-based instead of database-based storage would be a big deal at this point...

问题回答

You re not giving much information on the environment you use, other than the tags you attached to your post.

What you should do is use the streaming features of your database server (and the connection layer you use) to send the data through a stream.

It seems that only seekable streams are supported, so you should write the uploading file to a temporary file first (using streams, don t buffer the file in memory) and then use a FileStream to insert the file into the database.

You ll need a client application (Silverlight, Java Applet, Flash, etc) which break your file in small chunks and send that to server;

At server, you ll need to call your database and save that chunk; in my tests, best performance came from UPDATETEXT

Note you ll need to do something similar to provide file downloads; I suggest to read about READTEXT function, pretty same than UPDATETEXT, and to use it inside an IHttpAsyncHandler

I don t think it would be practical to store files that large in a database, especially by uploading (I m assuming the upload is through a browser.)

At a minimum, you d want to upload the file to a holding folder on the server and move it into the database once it s fully uploaded. Otherwise you d have to figure out how to chunk the bytestream into the database using multiple updates. I don t think you can append to a BLOB that s already in a record.

UPDATE: It looks like I may have been wrong about the chunking. SQL Server supports UPDATETEXT.

I still think uploading files that large and "streaming" them into the database is fragile and presents reliability issues.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签