1. 程式人生 > >Response下載檔案時,火狐瀏覽器檔名亂碼問題解決方法

Response下載檔案時,火狐瀏覽器檔名亂碼問題解決方法

        #region 下載檔案 add by ysc 20170104
        /// <summary>
        /// 下載檔案
        /// </summary>
        /// <returns></returns>
        public static void DownloadFile(string fullPath, HttpRequest Request, HttpResponse Response)
        {
            FileInfo file = new FileInfo(fullPath);
            string name = file.Name;
            if (Request.UserAgent.ToLower().IndexOf("msie") > -1) //ie瀏覽器
            {
                name = HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8);
            }

            if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) //火狐瀏覽器
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + name + "\"");
            }
            else //其他瀏覽器,如谷歌瀏覽器
            {
                Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
            }
            Response.Charset = "utf-8";
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        #endregion