Pregunta

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.

¿Fue útil?

Solución

try something like this:

Response.AddHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(fileName, Encoding.UTF8) + ".zip\"");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top