附件下载,中文文件名乱码如何解决???
問:
我寫了個程序,里面有文件要作為附件下載,英文文件名都正常,但中文文件名就會亂碼,不知道如何解決,望高手指點。
代碼如下:
=============================
Response.Clear();
Response.BufferOutput=true;
Response.Charset="utf-8";//此處用“GB2312”也不行
Response.AppendHeader("Content-Disposition","attachment;filename=測試.xls");
Response.ContentType = "application/vnd.ms-excel";
FileInfo mf=new FileInfo(sFile);
FileStream fs=mf.OpenRead();
Response.WriteFile(fs.Handle,0,mf.Length);
fs.Close();
回答1:
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對話框指定默認文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
// 添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
// 把文件流發送到客戶端
Response.WriteFile(file.FullName);
// 停止頁面的執行
Response.End();
回答2:
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
回答3:
GB2312也不行么?,實在不行你重新寫一下試試
回答4:
這樣寫就OK了
Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(測試.xls));
回答5:
編碼問題
回答6:
再給你回復一次,我faint
string pathfile = @"F:/新建文件夾/1.txt"; //pathfile 是要下載的文件名稱 
System.IO.FileInfo file = new System.IO.FileInfo(pathfile);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
回答7:中文文件名的問題已解決,謝謝大家的幫忙。?
總結
以上是生活随笔為你收集整理的附件下载,中文文件名乱码如何解决???的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 简述闭包
- 下一篇: linux查服务器硬件PN号,查看lin
