c# - How to download large files, stored in database...fluent nhibernate -


using: c#, .net 3.5 web forms application, fluent nhibernate 1.1.0.685, sql server 2008r2

i have web app allows users upload files , attach them "cases" working. files stored in database varbinary(max).

here code i'm using download files:

...  if (!siteuserservice.haspermission(domain.siteuserservice.sitepermissions.modifycase, currentuser))     this.accessdenied();  string attachmentid = request.querystring["f"].tostring(); downloadfileresponse response = caseservice.retrieveattachment(attachmentid, currentuser.id); downloadattachment(response.attachment.contenttype, response.attachment.filename, response.filebytes);  ...  protected void downloadattachment(string mimetype, string filename, byte[] file) {     response.clear();     response.contenttype = mimetype;     response.addheader("content-disposition", string.format("attachment;filename=\"{0}\"", filename));     response.binarywrite(file);     response.flush();     response.end(); } 

this works fine smaller files. couple files uploaded yesterday, sizes (in bytes): 2230165, 2104051, 1024274, , 2202318. when users try download these files receive "request timed out" error. majority of files around 45572 bytes.

the application in question resides in dmz, using wcf services data access, no direct sql calls, solution this won't work.

i don't have physical file in file system, don't think code (below) work:

system.io.stream stream = null; byte[] buffer = new byte[10000]; int length; long datatoread;  string filepath = context.request.physicalpath; string filename  = system.io.path.getfilename(filepath);  try {     stream = new system.io.filestream(filepath, system.io.filemode.open,     system.io.fileaccess.read,system.io.fileshare.read);      datatoread = stream       context.response.contenttype = "application/octet-stream";     context.response.addheader("content-disposition", "attachment; filename=" + filename);      while(datatoread > 0)     {         if(context.response.isclientconnected)         {             length = stream (buffer, 0, 10000);             context.response.outputstream.write(buffer, 0, length);              context.response.flush();              buffer= new byte[10000];             datatoread = datatoread - length;         }         else         {             datatoread = -1;         }     } } catch(exception ex) {     throw(ex); } {     if(stream != null)     {         stream ();     } } 

any suggestions how can allow files downloaded in "chunks"?

edit: looked @ this solution, don't know how implement byte[] db.

you have use streaming api of sql server can wrapped stream. use this

var sqlconn = (sqlconnection)session.connection; blobstream blob = new blobstream(sqlconn, sqlconn.begintransaction(), "dbo", "uploads", "filedata", "id", id); bufferedstream bufferedblob = new bufferedstream(blob, 8040);  // use bufferedblob stream , context stream 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -