简体   繁体   中英

Handler response wrong filename encoding

My handler:

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) {

    }
}

If my filename in Russian, the result is like 'Общая информация.docx' with wrong encoding.

Database return correct value.

尝试这样的事情:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM