c# - Downloading a dynamic file -
i creating file dynamically in web application. want send file user, many sites when download file.
i first tried approach @ download/stream file url - asp.net. didn't work because i'm in class that's layer removed aspx web page; result file overwrote itself, alternating unable create proper request/response objects.
then tried approach:
try { webclient myclient = new webclient(); string basefile = path.getfilename( file_name ); myclient.downloadfile( file_name, basefile ); } catch (webexception we) { string message = we.message; }
this raised exception: access path 'c:\program files (x86)\iis express\file-being-sent.ext' denied.
so, how stream file?
eta previous attempt:
//create webrequest file httpwebrequest filereq = (httpwebrequest)httpwebrequest.create( url ); //send request url //create response request httpwebresponse fileresp = (httpwebresponse)filereq.getresponse(); //if (filereq.contentlength > 0) // fileresp.contentlength = filereq.contentlength; //get stream returned response //stream = new stream(); writer = new streamwriter( file_name ); //stream = writer.basestream.length // prepare response client. resp client response var resp = httpcontext.current.response; //indicate type of data being sent //resp.contenttype = "application/octet-stream"; resp.contenttype = "application/zip"; //name file resp.addheader( "content-disposition", "attachment; filename=\"" + path.getfilename( file_name ) + "\"" ); resp.addheader( "content-length", writer.basestream.length.tostring() ); // verify client connected. if (resp.isclientconnected) { resp.write( writer ); resp.flush(); }
i haven't figured out pass call httpcreaterequest()
.
eta2: here's i'm presently using. various sof posts should prompt user open or save dialog, still not me.
streamreader reader = new streamreader( file_name ); var resp = httpcontext.current.response; //indicate type of data being sent resp.contenttype = "application/zip"; resp.appendheader( "content-disposition", "attachment; filename=\"" + path.getfilename( file_name ) + "\"" ); resp.appendheader( "content-length", reader.basestream.length.tostring() ); // verify client connected. if (resp.isclientconnected) { resp.clear(); resp.transmitfile( file_name ); //does not buffer memory, therefore scales better large files , heavy usage resp.flush(); }
eta3: iis trace log reports exception, system.web.httpexception: server cannot set content type after http headers have been sent.
not make sense me, i'm setting content type before appending headers.
stack trace is:
[httpexception (0x80004005): server cannot set content type after http headers have been sent.] system.web.httpresponse.set_contenttype(string value) +9681570 system.web.httpresponseinternalwrapper.set_contenttype(string value) +41 system.web.ui.pagerequestmanager.renderpagecallback(htmltextwriter writer, control pagecontrol) +139 system.web.ui.control.renderchildreninternal(htmltextwriter writer, icollection children) +268 system.web.ui.control.renderchildren(htmltextwriter writer) +8 system.web.ui.page.render(htmltextwriter writer) +29 system.web.ui.control.rendercontrolinternal(htmltextwriter writer, controladapter adapter) +57 system.web.ui.control.rendercontrol(htmltextwriter writer, controladapter adapter) +100 system.web.ui.control.rendercontrol(htmltextwriter writer) +25 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +1386
eta4: moved streaming aspx page's code-behind file, has direct access response
object. i'm still getting same exception. i'm @ loss explain why accepted approach complaining me response has been sent, or otherwise why code won't prompt user download file.
just move content type , header code after resp.clear()
using (streamreader reader = new streamreader( file_name )) { var resp = httpcontext.current.response; resp.bufferoutput = true; //this article //resp.buffer = true; //this msdn comments // verify client connected. if (resp.isclientconnected) { resp.clear(); resp.clearheaders(); //indicate type of data being sent resp.contenttype = "application/zip"; resp.appendheader( "content-disposition", "attachment; filename=\"" + path.getfilename( file_name ) + "\"" ); resp.appendheader( "content-length", reader.basestream.length.tostring() ); resp.transmitfile( file_name ); //does not buffer memory, therefore scales better large files , heavy usage resp.end(); } }
Comments
Post a Comment