c# - Download document -
right i'm working project there need make download button. works fine, can click save or save when want download file, when click open, nothing happens, why that?
string path = filepath.tostring(); fileinfo file = new fileinfo(path); if (file.exists) { response.clear(); response.addheader("content-disposition", "attachment; filename=" + file.name); response.addheader("content-length", file.length.tostring()); response.contenttype = "application/msword"; //octet-stream response.writefile(file.fullname); response.end(); } update * not working open save
private void setworddocument() { string strfilename = cleanup(labelfirstname.text + "_" + labellastname.text + "_" + datetime.now.tostring("yyyy-mm-dd") + "." + dropdownlistdownloadcv0.selecteditem.text); object fs = server.mappath("~/upload/") + strfilename; using (var db = new knowitcvdbentities()) { var theempl = (from p in db.employees p.username == strusername select p).firstordefault(); if (theempl != null) { object missing = missing.value; object start1 = 0; var wordapp = new applicationclass(); var mydoc = wordapp.documents.add(ref missing, ref missing, ref missing, ref missing); object donotsavechanges = wdsaveoptions.wddonotsavechanges; range rng = mydoc.range(ref start1, ref missing); try { const char newline = (char)11; mydoc.saveas(ref fs, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); } { mydoc.save(); //mydoc.close(ref donotsavechanges, ref missing, ref missing); wordapp.quit(ref donotsavechanges,ref missing,ref missing); mydoc = null; wordapp = null; system.runtime.interopservices.marshal.releasecomobject(mydoc); system.runtime.interopservices.marshal.releasecomobject(wordapp); gc.collect(); system.io.stream istream = null; // buffer read 10k bytes in chunk: byte[] buffer = new byte[10000]; // length of file: int length; // total bytes read: long datatoread; // identify file download including path. string filepath = fs.tostring(); // identify file name. string filename = system.io.path.getfilename(filepath); try { // open file. istream = new system.io.filestream(filepath, system.io.filemode.open, system.io.fileaccess.read, system.io.fileshare.read); // total bytes read: datatoread = istream.length; response.contenttype = "application/msword"; response.addheader("content-disposition", "attachment; filename=" + filename); //application/octet-stream // read bytes. while (datatoread > 0) { // verify client connected. if (response.isclientconnected) { // read data in buffer. length = istream.read(buffer, 0, 10000); // write data current output stream. response.outputstream.write(buffer, 0, length); // flush data html output. response.flush(); buffer = new byte[10000]; datatoread = datatoread - length; } else { //prevent infinite loop if user disconnects datatoread = -1; } } } catch (exception ex) { // trap error, if any. response.write("error : " + ex.message); } { if (istream != null) { //close file. istream.close(); } response.close(); } what have forgotten?
i remove line response.addheader("content-length", file.length.tostring());
please follow rfc2616:
the content-length entity-header field indicates size of entity-body, in decimal number of octets, sent recipient or, in case of head method, size of entity-body have been sent had request been get.
hope help.
edit:
by way, should make sure filepath variable using @ here apply server.mappath , file.name should not contains white space.
edit2:
base on comment, response.writefile not solution work , has many problem large file can take reference @ here . should apply response stream follow link below read file stream , put memory , send client link below:
how put webresponse memory stream?
or
http://forums.asp.net/t/1794834.aspx/1
edit 3:
your code posted here still not release ms word correctly, still keeping document that's reason why got exception comments.
please follow link below resolve problem.
disposing of microsoft.office.interop.word.application
or
http://code.msdn.microsoft.com/office/csautomateword-f422cae5
Comments
Post a Comment