ASP tutorial. NET xls file Download feature example
Provide two asp.net tutorial c file Download instance code, if you directly connect XLS file, will open in the browser, now we use. NET to download the instance.
*/
private void Page_Load (object sender, System.EventArgs e)
{
if (!page.ispostback)
{
string filepath = "D:www.111cn.net.xls";
filepath = "D:test.xls";
System.IO.FileStream fs = System.io.file.openread (filepath);
byte[] Filedata = new Byte[fs.length];
Fs.read (filedata, 0, (int) (fs.length));
Fs.close ();
Response.AddHeader ("Content-type", "application/vnd.ms-excel");
string Saveasfilename = "Yoursaveasfilename";
saveasfilename = encode (saveasfilename);
//saveasfilename = Httputility.urldecode ( Saveasfilename, system.text.encoding.getencoding ("Shift-jis"));
Response.AddHeader ("Content-disposition", "inline;filename=" + Saveasfilename);
Response.AddHeader ("Content-length", filedata.length.tostring ());
Response.BinaryWrite (Filedata);
Response.End ();
}
}
Method Two
public static void DownloadFile (string filepath)
{
String str = httpcontext.current.request.servervariables["Appl_physical_path"];
string path = str + "www.111cn.net" + filepath;
FileInfo file = new FileInfo (path);
httpcontext.current.response.contentencoding = system.text.encoding.getencoding ("Utf-8"); Solve Chinese garbled
Httpcontext.current.response.addheader ("Content-disposition", "attachment; Filename= "+ httpcontext.current.server.urlencode (file.name)); Solve the Chinese file name garbled
Httpcontext.current.response.addheader ("Content-length", file.length.tostring ());
Httpcontext.current.response.contenttype = "Appliction/octet-stream";
Httpcontext.current.response.writefile (File.fullname);
Httpcontext.current.response.end ();
}