c# - Download an Excel file -


i have read past posts here on how download excel file website. so, have setup below code:

string path = mappath(fname); string name = path.getfilename(path); string ext = path.getextension(path); string type = "application/vnd.ms-excel";   if (forcedownload) {     response.appendheader("content-disposition",         "attachment; filename=" + name); }  if (type != "") {     response.contenttype = type;     response.writefile(path);     response.end(); } 

however, no download dialog box.

i try both in ie 8 , firefox 10.0.2.
file there, it's not locked, , it's not set read only.
i'm not sure went wrong.

according this link, need add line:

strfilename = path.getfilename(path); response.transmitfile( server.mappath(strfilename) ); 

this cause open / save dialog box pop filename of sailbig.jpg default filename preset.

this of course assumes you're feeding file exists. if need feed dynamically generated - image [or file] generated in memory - can use response.binarywrite() stream byte array or write output directly in response.outputstream.

edit:

microsoft's msdn site has detailed explanation file downloading. includes both samples java , .net applications, concept same:

  1. get response.
  2. with response:
    1. set content type "application/octet-stream" (it means there's no application open file).
    2. set header "content-disposition", "attachment; filename=\"" + + "\"".
    3. write file content response.
  3. close response.

so, looking @ msdn asp.net file download, you're lacking 2.3 step. you're writing file name response.

// transfer file byte-by-byte response object system.io.fileinfo filetodownload = new     system.io.fileinfo("c:\\downloadjsp\\downloadconv\\myfile.txt"); response.flush(); response.writefile(filetodownload.fullname); 

with example download file successfully, of course if can file no problems :).

edit 2:

the html component used download file must regular html request. ajax request download file won't work. microsoft explains here. , main quote:

its impossible attach event before , after download through javascript. browser doesn't allow type of events security reasons.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -