问题描述
我用asp.net实现了一个文件下载的功能,当被下载文件的文件名为字母时,被下载到本地后文件名正常,但是当被下载文件的文件名是中文名时,下载到本地后,文件名发生了乱码,恳请大侠指教?我的下载实现代码如下:protectedvoidbtnDownLoad_Click(objectsender,EventArgse){try{stringpath="/Files";stringmapPath=Server.MapPath(Request.ApplicationPath+path);//生成服务器上要下载的文件所在文件夹的路径stringfileName="testFile.doc";//names;//要下载的文件的文件名//文件路径(如果你确定文件在服务器上的路径例如C:webdownloadfile1.txt,可以直接写stringfilePath="C:\web\download\file1.txt".但是不推荐)stringfilePath=System.IO.Path.Combine(mapPath,fileName);FileInfoinfo=newFileInfo(filePath);//打开文件流longfileSize=info.Length;//指定文件大小//下载操作Response.Clear();Response.ContentType="application/octet-stream";Response.AddHeader("Content-Disposition","attachement;filename="+fileName);Response.AddHeader("Content-Length",fileSize.ToString());Response.WriteFile(filePath,0,fileSize);Response.Flush();Response.Close();}catch(Exceptionex){Response.Write("<scriptlanguage=javascript>alert("下载错误:"+ex.Message+"!");</script>");}
解决方案
解决方案二:
usingSystem;usingSystem.Collections;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Xml.Linq;namespaceAsiastar.NR.Ajax{///<summary>///$codebehindclassname$的摘要说明///</summary>[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]publicclassHandler1:IHttpHandler{publicvoidProcessRequest(HttpContextcontext){context.Response.ContentType="text/plain";stringid=context.Request["id"].ToString();//获取资源的编号System.IO.StreamiStream=null;byte[]buffer=newByte[10000];intlength;longdataToRead;NRBLL.Filebf=newAsiastar.NRBLL.File();Guidguid=newGuid(id);if(bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"]!=null)//判断数据库路径是否存在{stringfilepath=bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString();//获取资源完整路径D:资源文件600cc139-14cf-448e-9e50-daa972d35e01.jpgstringOidfilename=bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FileNam"].ToString();//旧文件名称//stringfilename=System.IO.Path.GetFileName(filepath);//获取文件名称+后缀名600cc139-14cf-448e-9e50-daa972d35e01.JPG//intindex=filepath.IndexOf(".");//stringfiletype=filepath.Substring(index).ToLower();//后缀名//stringnewfilename=Oidfilename;//stringfilepath1=bf.FN_SerchPathByFileId(guid).Tables[0].Rows[0]["FilePath"].ToString().Substring(0,filepath.Length-8);try{stringfileName=HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(Oidfilename));//解码(注意这里2层解码)Oidfilename=Oidfilename.Replace("+","%20");//将“+”替换成“空格”iStream=newSystem.IO.FileStream(filepath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.Read);dataToRead=iStream.Length;context.Response.ContentType="application/octet-stream";context.Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(Oidfilename,System.Text.Encoding.UTF8));//下载的时候下载原来的文件名称while(dataToRead>0){if(context.Response.IsClientConnected){length=iStream.Read(buffer,0,10000);context.Response.OutputStream.Write(buffer,0,length);context.Response.Flush();buffer=newByte[10000];dataToRead=dataToRead-length;}else{dataToRead=-1;}}}catch(Exceptionex){NR.Error.Log.LogType(ex.ToString());}finally{if(iStream!=null){iStream.Close();}}}else{NR.Error.Log.LogType("找不到文件!");}}publicboolIsReusable{get{returnfalse;}}}}
编码格式没有指定然后参考我上面的下载