gpt4 book ai didi

c# - .aspx 页面 Response.BinaryWrite 图像在 IE7 上

转载 作者:行者123 更新时间:2023-11-30 21:25:29 24 4
gpt4 key购买 nike

我维护一个应用程序,它有一个 .aspx 页面,该页面从数据库加载图像并使用 Response.BinaryWrite() 将其写回客户端。这在不久前还非常有效。有两件事发生了变化,我们将应用程序升级到 .NET 3.5,他们将工作中的所有计算机升级到 IE7。

在 Firefox 上一切正常,但我在 IE7 中得到的只是一个红色的 X。所以我认为这个问题与 IE7 有关?某处是否有安全设置会阻止它从 .aspx 表单加载图像?它已设置为根据内容类型而非扩展名显示。

这是一些代码。就像我说的,我只是维护这个应用程序而不是编写它。我知道使用 Session 不是一个很好的方法,但这是我所拥有的,switch 语句只是一个“wtf?”。

<asp:image id="imgContent" runat="server" Visible="true" ImageUrl="ProductContentFormImage.aspx"></asp:image>

protected void Page_Load(object sender, System.EventArgs e)
{
Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
byte[] content = (byte[]) hshContentBinary["content"];
string extension = (string) hshContentBinary["extension"];


string contentTypePrefix = "application";
switch(extension.ToLower())
{
case "gif":
case "jpg":
case "bmp":
contentTypePrefix = "image";
break;
case "tif":
contentTypePrefix = "image";
break;
case "tiff":
contentTypePrefix = "image";
break;
case "eps":
contentTypePrefix = "image";
break;
default:
Response.AppendHeader(
"Content-disposition",
"attachment; filename=content." + extension );
break;
}
Response.ContentType = contentTypePrefix + "/" + extension;
Response.BinaryWrite(content);
}

编辑:

好的,我听从了您的建议,并通过更多的研究将方法更改为以下方法,但它仍然不起作用。

protected void Page_Load(object sender, System.EventArgs e)
{
Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
byte[] content = (byte[]) hshContentBinary["content"];
string extension = (string) hshContentBinary["extension"];
string contentType;
string contentDisposition = "inline; filename=content." + extension;

Response.ClearContent();
Response.ClearHeaders();
Response.Clear();

switch(extension.ToLower())
{
case "gif":
contentType = "image/gif";
break;
case "jpg":
case "jpe":
case "jpeg":
contentType = "image/jpeg";
break;
case "bmp":
contentType = "image/bmp";
break;
case "tif":
case "tiff":
contentType = "image/tiff";
break;
case "eps":
contentType = "application/postscript";
break;
default:
contentDisposition = "attachment; filename=content." + extension;
contentType = "application/" + extension.ToLower();
break;
}

Response.Buffer = true;
Response.Expires = 0;
Response.ContentType = contentType;
Response.AddHeader("Content-Length", content.Length.ToString());
Response.AddHeader("Content-disposition", contentDisposition);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(content);
Response.End();
}

最佳答案

您的 MIME 类型不正确。这样效果更好,至少对于图像而言:

string contentType = "application/" + extension.ToLower();
switch(extension.ToLower()) {
case "gif": contentType = "image/gif"; break;
case "jpg":
case "jpeg":
case "jpe": contentType = "image/jpeg"; break;
case "bmp": contentType = "image/bmp"; break;
case "tif":
case "tiff": contentType = "image/tiff"; break;
case "eps": contentType = "application/postscript"; break;
default:
Response.AppendHeader(
"Content-disposition",
"attachment; filename=content." + extension );
break;
}
Response.ContentType = contentType;

Hera是mime类型和文件扩展名,如果需要添加更多: http://www.w3schools.com/media/media_mimeref.asp

关于c# - .aspx 页面 Response.BinaryWrite 图像在 IE7 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/630682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com