Using System; Using System.Collections.Generic; Using System.Linq; Using System.Web; Using System.IO;
Using ELEMENTLIBRARY.BLL; Using Elementlibrary.model; Using System.Text; /* * liuh * Descr: Download processing downloadfile.ashx * addtime:2014/8/26 * lastmodifytime:2014/8/27 */ namespace ElementLibrary.Web.Action { //<summary> ///downloadfile Summary Description & nbsp; //</summary> public class Downloadfile:ihttphandler {& nbsp; ELEMENTDETAILBLL ELEMENTDETAILBLL = new ELEMENTDETAILBLL (); public void ProcessRequest (HttpContext context) { String Strfunid = context. request.querystring["id"]. ToString ();//Gets the method ID (component ID) MODEL. Elementdetailinfo elementdetailinfo = elementdetailbll.getallcontent (Strfunid);//client-saved file name &NBSP;&NBSP;&NBSP;&Nbsp; string fileName = Elementdetailinfo.filename; string strFilePath = Elementdetailinfo.filepath ; string FilePath = context. Server.MapPath (strFilePath);//Path FileInfo FileInfo = new FileInfo (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 encode filename = 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 string tohexstring (string s) & nbsp; { char[] chars = S. ToCharArray (); StringBuilder builder = new StringBuilder (); for (int index = 0; index < chars. Length; index++) { bool Needtoencode = needtoEncode (Chars[index]); if (needtoencode) { string encodedstring = tohexstring (Chars[index]); Builder. Append (encodedstring); } Else { Builder. Append (Chars[index]); } }
Return builder. ToString (); }
<summary>///Specify whether a character should be encoded///</summary>//<param name= "CHR" ></param& Gt <returns></returns> private static bool Needtoencode (char chr) {string Reserv Edchars = "$-_.+!* ' (),@=&";
if (Chr > 127) return true; if (char. Isletterordigit (CHR) | | Reservedchars.indexof (CHR) >= 0) return false;
return true; }
//<summary> // For non-English string encoding ///</summary> //<param name= "CHR" ></param> //<returns>< /returns> private static string tohexstring (char chr) { utf8encoding UTF8 = new UTF8Encoding (); byte[] encodedbytes = UTF8. GetBytes (Chr. ToString ()); StringBuilder builder = new StringBuilder (); for (int index = 0; index < encodedbytes.length; index++) { Builder. AppendFormat ("%{0}", Convert.ToString (Encodedbytes[index], 16)); } return builder. ToString (); }
public bool IsReusable {get {return false; } } } }
ASP. net file download file name garbled processing ~ ~