【问题标题】:Handler response wrong filename encoding处理程序响应错误的文件名编码
【发布时间】:2013-12-26 06:12:05
【问题描述】:

我的处理程序:

public void ProcessRequest(HttpContext context) {
    HttpRequest request = context.Request;
    HttpResponse response = context.Response;

    try {
        response.Clear();
        response.AddHeader("pragma", "no-cache");
        response.AddHeader("cache-control", "private");
        response.CacheControl = "no-cache";

        SqlConnection conn = new SqlConnection(connString);
        conn.Open();

        SqlCommand cmd = new SqlCommand("dbo.cpGetBinary", conn);
        cmd.Parameters.AddWithValue("id", request.Params["id"]);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();

        context.Response.ContentType = "text/rtf; charset=UTF-8";
        context.Response.Charset = Encoding.UTF8.EncodingName;
        response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", dReader["content_file"]));
        response.ContentType = "application/octet-stream";
        response.BinaryWrite((byte[])dReader["binary_value"]);
        dReader.Close();
        conn.Close();
        response.End();
    }
    catch (Exception ex) {

    }
}

如果我的文件名是俄语,结果类似于 'Общая информация.docx' 编码错误。

数据库返回正确的值。

【问题讨论】:

  • 也许尝试将 dReader["content_file"] 编码为 UTF-8。 byte[] bytes = Encoding.Default.GetBytes(myString); myString = Encoding.UTF8.GetString(bytes);
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# asp.net encoding response httphandler


【解决方案1】:

试试这样的:

Response.AddHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".zip\"");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2011-12-12
    • 2017-05-25
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多