ASP. net file download fix file name garbled

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.IO;usingELEMENTLIBRARY.BLL;usingElementlibrary.model;usingSystem.Text;/** liuh * Descr: Download processing downloadfile.ashx * ADDTIME:2014/8/26 * LASTMODIFYTIME:2014/8/27*/namespaceelementlibrary.web.action{/// <summary>    ///Summary description of DownLoadFile/// </summary>     Public classDownloadfile:ihttphandler {ELEMENTDETAILBLL ELEMENTDETAILBLL=NewELEMENTDETAILBLL ();  Public voidProcessRequest (HttpContext context) {stringStrfunid = context. request.querystring["ID"]. ToString ();//Get method ID (component ID)MODEL. Elementdetailinfo elementdetailinfo = elementdetailbll.getallcontent (Strfunid);//file name saved by the client            stringFileName =Elementdetailinfo.filename; stringstrFilePath =Elementdetailinfo.filepath; stringFilePath = context. Server.MapPath (strFilePath);//PathFileInfo FileInfo =NewFileInfo (FilePath); Context.            Response.Clear (); Context.            Response.clearcontent (); Context.           Response.ClearHeaders (); //fix file name garbled (Liuh addtime:2014/8/28)            if(Context. Request.UserAgent.Contains ("MSIE") || Context. Request.UserAgent.Contains ("MSIE"))            {                //If clients use Microsoft Internet Explorer, they need to encodeFileName =tohexstring (fileName); } context. Response.AddHeader ("content-disposition","attachment;filename="+fileName); Context. Response.AddHeader ("Content-length", fileInfo.Length.ToString ()); Context. Response.AddHeader ("content-transfer-encoding","binary"); Context. Response.ContentType="Application/octet-stream"; Context. Response.ContentEncoding= System.Text.Encoding.GetEncoding ("Utf-8"); Context.            Response.WriteFile (Fileinfo.fullname); Context.            Response.Flush (); //Fileinfo.delete ();context.        Response.End (); }        /// <summary>        ///encode non-English characters in a string/// </summary>        /// <param name= "s" ></param>        /// <returns></returns>         Public Static stringTohexstring (strings) {Char[] chars =S.tochararray (); StringBuilder Builder=NewStringBuilder ();  for(intindex =0; Index < chars. Length; index++)            {                BOOLNeedtoencode =Needtoencode (Chars[index]); if(needtoencode) {stringEncodedString =tohexstring (Chars[index]); Builder.                Append (encodedstring); }                Else{Builder.                Append (Chars[index]); }            }            returnBuilder.        ToString (); }        /// <summary>        ///Specifies whether a character should be encoded/// </summary>        /// <param name= "CHR" ></param>        /// <returns></returns>        Private Static BOOLNeedtoencode (CharCHR) {            stringReservedchars ="$-_.+!* ' (),@=&"; if(Chr >127)                return true; if(Char. Isletterordigit (CHR) | | Reservedchars.indexof (CHR) >=0)                return false; return true; }        /// <summary>        ///encoding for non-English strings/// </summary>        /// <param name= "CHR" ></param>        /// <returns></returns>        Private Static stringTohexstring (CharCHR) {utf8encoding UTF8=Newutf8encoding (); byte[] encodedbytes =UTF8. GetBytes (Chr.            ToString ()); StringBuilder Builder=NewStringBuilder ();  for(intindex =0; Index < encodedbytes.length; index++) {Builder. AppendFormat ("%{0}", Convert.ToString (Encodedbytes[index], -)); }            returnBuilder.        ToString (); }         Public BOOLisreusable {Get            {                return false; }        }    }}

ASP. net file download fix file name garbled

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.