0

我在模块中有以下代码,可将 Response 的输入引导回浏览器。问题是我有几个来自 3rd 方应用程序的 PNG 文件没有正确呈现。还有其他 PNG 和 JPG 文件可以正确渲染。问题文件的 Content-Type 以文本形式返回,导致它无法运行正确的输出类型代码。有没有人见过这个?关于如何解决这个问题的任何想法?

byte[] responsearr = ReadFully(response.GetResponseStream());
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
string responseStr = encoding.GetString(responsearr);

HttpContext.Current.Response.ContentType = response.ContentType;

if (response.ContentType.Contains("image") || response.ContentType.Contains("document"))
{
    if (response.ContentType.Contains("image"))
        Debug.WriteLine(encoding.GetString(responsearr));
    HttpContext.Current.Response.OutputStream.Write(responsearr, 0, responsearr.Length);
}
else
{
    HttpContext.Current.Response.Write(responseStr);
}

HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

在我可以向 Apache 管理员验证 MIME 类型分配之前,我已将代码更新为以下内容。

void RouteRequest(ProxyMappingElement mapping)
{
    //does a whole bunch of stuff between here and there .... 

    // Important problem piece

    byte[] responsearr = ReadFully(response.GetResponseStream());
    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
    string responseStr = encoding.GetString(responsearr);
    HttpContext.Current.Response.ContentType = GetContentType(response);

    if (HttpContext.Current.Response.ContentType.Contains("image") ||       HttpContext.Current.Response.ContentType.Contains("document"))
    {
        HttpContext.Current.Response.OutputStream.Write(responsearr, 0, responsearr.Length);
    }
    else
    {
        HttpContext.Current.Response.Write(responseStr);
    }

    HttpContext.Current.Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}

private string GetContentType(HttpWebResponse response)
{
    return HttpContext.Current.Request.Url.ToString().Substring(
        HttpContext.Current.Request.Url.ToString().Length - 3, 3).ToLower().Equals("png")
               ? (HttpContext.Current.Response.ContentType = "image/png")
               : (HttpContext.Current.Response.ContentType = response.ContentType);
}
4

0 回答 0