ASP. net file download file name garbled processing ~ ~

Source: Internet
Author: User

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 ~ ~

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.