asp.net xls file Download function example

Source: Internet
Author: User
Tags httpcontext

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.