我维护一个具有 .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# - IE7 上的 .aspx 页面 Response.BinaryWrite 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/630682/