C # compatible with major browser file Download example detailed

Source: Internet
Author: User
This article for you to share an example code about the CSS implementation of file download function, the need for a friend to refer to it

1. CSS Code

public void Downfile (string filePath, String fileName) {///FilePath file path For example:/file/record. xlsx//FileName file name for example: Record. xlsx (to Suffix OH) Encoding Encoding; Declaration code string OutputFileName; Output name Debug.Assert (HttpContext.ApplicationInstance.Request.UserAgent! = NULL, " HttpContext.ApplicationInstance.Request.UserAgent = null "); string browser = HttpContext.ApplicationInstance.Request.UserAgent.ToUpper ();//Microsoft's browser and IE filter if (browser. Contains ("MS") && browser. Contains ("IE")) {OutputFileName = Httputility.urlencode (filePath); encoding = Encoding.default;} Firefox else if (browser. Contains ("FIREFOX")) {outputfilename = Filename;encoding = encoding.getencoding ("GB2312");} Else{outputfilename = Httputility.urlencode (fileName); encoding = Encoding.default;} String absolufilepath = Server.MapPath (FilePath); Get the upload file path FileStream fs = new FileStream (Absolufilepath, FileMode.Open); byte[] bytes = new byte[(int) fs. Length];fs. Read (bytes, 0, bytes. Length); fs. Close (); Close the stream and release the resource HttpContext.ApplicationInstance.Response.CleAR (); HttpContext.ApplicationInstance.Response.Buffer = true; HttpContext.ApplicationInstance.Response.ContentEncoding = encoding; HttpContext.ApplicationInstance.Response.AddHeader ("Content-disposition", String. Format ("attachment; Filename={0} ", String. IsNullOrEmpty (outputfilename)? DateTime.Now.ToString ("Yyyymmddhhmmssfff"): OutputFileName); Response.BinaryWrite (bytes); Response.Flush (); HttpContext.ApplicationInstance.Response.End ();}

2. HTML code

Front-end HTML to write a tag is good:<a href='DownFile' target='_blank'>文件下载</a>

【相关推荐】

1. 特别推荐:“php程序员工具箱”V0.1版本下载

2. ASP免费视频教程

3. 李炎恢ASP基础视频教程

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.