各位高手:
    我在日文系统下开发一个服务器端csv文件下载到本地文件 文件名为日文但出现乱码问题
   代码如下  :
    string  strFilePath ;
FileStream fsTemp ;
byte[] buffer ; strFilePath = Server.MapPath("./");
File.Copy(strFilePath + "cheyj1.csv", strFilePath + "cheyj.csv");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=詳細設計書.csv");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift-JIS"); fsTemp = File.OpenRead(strFilePath + "詳細設計書.csv");
buffer=new byte[fsTemp.Length - 1];
//ReDim buffer(fsTemp.Length - 1);
fsTemp.Read(buffer, 0, buffer.Length);
fsTemp.Close();
File.Delete(strFilePath + "詳細設計書.csv");
Response.BinaryWrite(buffer); Response.End();
请问应如何处理不回出现乱码

解决方案 »

  1.   

    我遇到过。这个是因为传输的时候,文字有个默认的编码形式,而这个形式不支持日文。做法是将编码进行一次转换。
    我们做的是将本地的文件传递到服务器,同样出现了传递日文文件的时候出现乱码,
    避免的例子代码如下:客户端:
    Imports System.Text.Encoding
    ……
    GetEncoding(UTF8.WebName).GetBytes(NewFileName)
    其中NewFileName为传输到服务器中需要保存的文件的文件名,为string类型,GetEncoding(UTF8.WebName).GetBytes(NewFileName)将其变为byte[]类型;
    服务器端:
    Imports System.Text.Encoding
    ……
    GetEncoding(UTF8.WebName).GetString(FileName)
    其中FileName为客户端传递过来的文件名。为byte[]类型,GetEncoding(UTF8.WebName).GetString(FileName)将其转换为string类型。大体的思路是:
    1,用GetEncoding(UTF8.WebName).GetBytes(NewFileName) 将客户端的文件名转换成utf8(日文下的认)的byte[]类型的文件名;
    2,将文件名(byte[]类型的)传递到服务器端;
    3,利用GetEncoding(UTF8.WebName).GetString(FileName)将服务器端获得的文件名(byte[]类型的)转换成string类型的文件名。ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemTextEncodingMembersTopic.htm
      

  2.   

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift-JIS");这种写发,好像不好用。试验一下我说的方法啊。
      

  3.   

    各位高手:
        这是我新写的代码:还是出现IE中的下载提示框为乱码:
     string  strFilePath ;
    FileStream fsTemp ;
    byte[] buffer ;strFilePath = Server.MapPath("./");
    string strfilename;
    string strname;
    strfilename="詳細設計書.csv" ;
    System.Text.UTF8Encoding AE = new System.Text.UTF8Encoding();
    byte[] input = AE.GetBytes(strfilename);strname=AE.GetString (input,0,input.Lenth)
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    response.Clear();
    Response.ContentType = "application/vnd.ms-excel; charset=UTF-8";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    Response.AddHeader("Content-Disposition", "attachment; filename="+strname);
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");fsTemp = File.OpenRead(strFilePath + "詳細設計書.csv");
    buffer=new byte[fsTemp.Length - 1];
    fsTemp.Read(buffer, 0, buffer.Length);
    fsTemp.Close();
    Response.BinaryWrite(buffer);Response.End();
     请再指点一下如何修改
    才能使下载的日文文件名在下载提示框不显示乱码
    谢谢
      

  4.   

    Dim fleInfo = New System.IO.FileInfo(Request.QueryString("PrintFile")+ ".csv")
    Response.Clear()
    Response.Charset = "Shift_JIS"
    Response.AddHeader("Content-Disposition", "attachment; filename=" & System.Web.HttpUtility.UrlEncode(Request.QueryString("ShowName")) + ".csv")
    Response.AddHeader("Content-Length", fleInfo.Length.ToString())
    Response.ContentType = "text/csv"
    Response.WriteFile(fleInfo.FullName)
    用此函数转码:
    System.Web.HttpUtility.UrlEncode()