C# .aspx 页面 Response.BinaryWrite 图像在 IE7 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/630682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.aspx page Response.BinaryWrite image on IE7
提问by jhunter
I maintain an application that has a .aspx page that loads on image from the database and uses Response.BinaryWrite() to write it back to the client. This worked perfectly not long ago. Two things have changed, we upgraded the application to .NET 3.5 and they upgraded all the computers at work to IE7.
我维护一个具有 .aspx 页面的应用程序,该页面从数据库加载图像并使用 Response.BinaryWrite() 将其写回客户端。这在不久前完全奏效。有两件事发生了变化,我们将应用程序升级到 .NET 3.5,他们将所有工作中的计算机升级到 IE7。
Everything works fine on Firefox, but all I get in IE7 is a red X. So I assume this issue is related to IE7? Is there a security setting somewhere that would stop it from loading images from a .aspx form? It's already set to display based on the content type and not the extension.
在 Firefox 上一切正常,但我在 IE7 中得到的只是一个红色的 X。所以我认为这个问题与 IE7 相关?是否有安全设置可以阻止它从 .aspx 表单加载图像?它已经设置为基于内容类型而不是扩展名显示。
Here is some of the code. Like I said, I just maintain this app and didn't write it. I know using Session is not a great way of doing it, but it's what I have and the switch statement is just a "wtf?".
这是一些代码。就像我说的,我只是维护这个应用程序,并没有编写它。我知道使用 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);
}
EDIT:
编辑:
OK, I followed your suggestions and through a little more research I have changed the method to the following, but it still doesn't work.
好的,我遵循了您的建议,并通过更多研究将方法更改为以下方法,但它仍然不起作用。
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();
}
采纳答案by jhunter
The image was corrupted in a way that IE7 could not display it, but Firefox could. The image was large it wouldn't fit on the screen and I didn't see where it was cut off.
图像已损坏,IE7 无法显示,但 Firefox 可以。图像很大,无法显示在屏幕上,而且我没有看到它被剪掉的地方。
Thanks for all your suggestions.
感谢您的所有建议。
回答by Eddie
You might try a...
你可以试试...
Response.ClearContent();
Response.ClearHeaders();
...right before...
...就在之前...
Response.ContentType = contentTypePrefix + "/" + extension;
We had to do that to get the correct file type association in IE7.
我们必须这样做才能在 IE7 中获得正确的文件类型关联。
回答by Chris Van Opstal
Try setting your content-disposition to inline instead of attachment, like:
尝试将您的内容处置设置为内联而不是附件,例如:
Response.AppendHeader(
"Content-disposition",
"inline; filename=content." + extension );
While most browsers are very flexible in this respect, "attachment" was intended to require further action by the user. IETF RFC 1806:
虽然大多数浏览器在这方面非常灵活,但“附件”旨在要求用户采取进一步行动。IETF RFC 1806:
The display of an attachment is generally construed to require positive action on the part of the recipient, while inline message components are displayed automatically when the message is viewed.
附件的显示通常被解释为要求接收方采取积极的行动,而在查看消息时会自动显示内嵌消息组件。
回答by Guffa
Your mime types are not correct. This works better, at least for the images:
您的 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 are mime types and file extensions, if you need to add more: http://www.w3schools.com/media/media_mimeref.asp
Hera 是 mime 类型和文件扩展名,如果您需要添加更多:http: //www.w3schools.com/media/media_mimeref.asp
回答by AnthonyWJones
回答by Sachin
I can tell you that this way of image loading works fine in IE7 (just had written same sorta code some time back). So, there could be following issues: 1) Try doing a Response.Clear() before setting the ContentyType and do a response.end in the end. 2) Make sure that your extension in the session is without the period (.) i.e. it should be just gif for .gifs, jpg for .jpg etc.
我可以告诉你,这种图像加载方式在 IE7 中运行良好(前段时间刚刚编写了相同的代码)。因此,可能存在以下问题: 1) 在设置 ContentyType 之前尝试执行 Response.Clear() 并在最后执行 response.end。2) 确保您在会话中的扩展名没有句号 (.),即对于 .gifs 应该只是 gif,对于 .jpg 应该是 jpg 等。
S
秒
回答by shahkalpesh
Try going to another website in IE7.
Does it show the images for any other website?
尝试在 IE7 中访问另一个网站。
它是否显示任何其他网站的图像?
If it doesn't show images, I am guessing that you might have some setting (e.g. web developer toolbar OR fiddler) to "not to load images".
如果它不显示图像,我猜您可能有一些设置(例如 Web 开发人员工具栏或提琴手)“不加载图像”。
回答by Chris
Your code is very complicated, why not simplify it a bit, so that you're only setting the content type?
你的代码很复杂,为什么不简化一下,让你只设置内容类型?
Here's code i use, and it works in all browsers.
这是我使用的代码,它适用于所有浏览器。
protected void Page_Load(object sender, EventArgs e)
{
byte[] jpg = .....
Response.Clear();
Response.ContentType = "image/jpeg";
Response.BinaryWrite(jpg);
Response.End();
}
回答by Ira Miller
Consider setting up your page with some caching including client side cache control headers. When you do this make sure that you set a different filename for each individual image
考虑使用一些缓存设置您的页面,包括客户端缓存控制标头。执行此操作时,请确保为每个单独的图像设置不同的文件名
contentDisposition = String.Format("attachment; filename=content{0).{1}",fileName, fileExtension);
and the client side cache headers
和客户端缓存标头
context.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(YourDatabaseImageOrConfigFile.CacheSeconds));
context.Response.Cache.SetOmitVaryStar(true);
context.Response.Cache.SetLastModified(YourDatabaseImage.ModifiedDate);
I would make this page into an ASHX http handler as well to get rid of the overhead of the page lifecycle.
我也会将此页面设为 ASHX http 处理程序,以消除页面生命周期的开销。
I have this whole thing written a few times over and can provide the code if needed. This image endpoint is on a site doing around 80 requests a second.
我把整件事都写了几次,如果需要,可以提供代码。此图像端点位于每秒处理大约 80 个请求的站点上。
回答by Jeff Ancel
I am not 100 percent sure about IE7, however I ran into a similar issue with ie8. The solution to my issue was to make sure that I covered the content type for "image/pjpeg". Not sure how many different coverages you need, but this one solved my issue.
我对 IE7 不是 100% 确定,但是我遇到了与 ie8 类似的问题。我的问题的解决方案是确保我涵盖了“image/pjpeg”的内容类型。不确定您需要多少种不同的保险,但这一项解决了我的问题。