处理程序响应错误的文件名编码

时间:2013-12-26 06:12:05

标签: c# asp.net encoding response httphandler

我的经纪人:

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'编码错误。

数据库返回正确的值。

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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