從服務器下載文件中文名亂碼解決方法


 try
        {
            string excelName = Request["excelName"].ToString(); //文件路徑
            string fileName = Request["fileName"].ToString(); //下載的文件名
            if(!(string.IsNullOrEmpty(excelName)|| string.IsNullOrEmpty(fileName)))
            {
                Response.Charset = "GB2312";
                Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                Response.ContentType="application/vnd.ms-excel";
                //火狐瀏覽器不需將中文文件名進行編碼格式轉換
                if (Request.ServerVariables["http_user_agent"].ToLower().IndexOf("firefox") == -1)
                {
                   excelName = HttpUtility.UrlEncode(excelName, System.Text.Encoding.UTF8);                 }
                Response.AppendHeader("Content-Disposition", "attachment;filename=" +excelName);
                Response.WriteFile(fileName);
                Response.Flush();
                Response.Close();
            }
        }
        catch
        { }

 注意判斷瀏覽器 IE需要將中文文件名編碼格式轉換,而firefox瀏覽器則不用,不然下載下來的中文文件名會亂碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM