Asp tutorial. net File Download Code
Response. clear ();
Response. buffer = true;
Response. contenttype = "application/vnd. ms-excel ";
Response. addheader ("content-disposition", "attachment; filename = billinginformation _" + this. ddl_mode.selectedvalue + "(" + system. datetime. now. tostring ("yyyymmdd") + "example .csv ");
Response. contentencoding = system. text. encoding. utf7;
Response. charset = "";
Response. write ("export excel file to clients sample ");
Response. end ();
Datatable filedt = filedao. getfilebyid (fileid );
Byte [] filebytes = (byte []) filedt. rows [0] ["filedata"];
String originalfilename = filedt. rows [0] ["filename"]. tostring ();
String randomfilename = guid. newguid (). tostring ();
// Download to disk
String filefullpath = this. server. mappath ("~ /Temp/"+ randomfilename );
File. writeallbytes (filefullpath, filebytes );
// Send to client
Response. clear ();
Response. clearcontent ();
Response. clearheaders ();
Response. contentencoding = system. text. encoding. getencoding ("UTF-8 ");
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (originalfilename, system. text. encoding. utf8 ));
Response. addheader ("content-transfer-encoding", "binary ");
Response. contenttype = "application/octet-stream ";
Response. writefile (filefullpath );
Response. flush ();
// Delete files on the disk
If (file. exists (filefullpath ))
File. delete (filefullpath );
Response. end ();