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