如何流式传输字节数组图像?

发布于 2024-11-13 12:20:19 字数 994 浏览 2 评论 0原文

由于某些奇怪的原因,一个使用了 5 年的内部 asp.net Web 应用程序(通过 IE6 使用)突然出现了问题,尽管没有进行任何代码更改。某些用户不会看到流回网络浏览器的某些图像。

我不知道为什么会突然发生这种情况,也不知道原因是什么 - 但是作为搜索的一部分,我正在考虑用于流回图像的代码中是否存在缺陷。

图像以字节数组的形式保存在内存中,然后使用以下代码流式传输回来。这是传回图像的最佳方式吗?

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

// Work out file type
switch( Path.GetExtension( imageFilename ).ToLower() )
{
    case ".jpg":
    case ".jpeg":
        Response.ContentType = "image/jpeg";
        break;
    case ".gif":
        Response.ContentType = "image/gif";
        break;
    default:
        Response.ContentType = "binary/octet-stream";
        break;  
}

Response.AddHeader("Content-Length", imageBytes.Length.ToString() );
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite( imageBytes );
Response.End();

页面中显示的不是图像,而是显示红十字。这种情况仅发生在页面上显示的动态生成的图像上,而不是静态链接的图像上。

如果我尝试在它自己的窗口中打开图像,我要么看到图像,要么看到一堆官样文章(我猜这意味着浏览器没有设置/拾取 MIME 类型)

For some strange reason, a five year old internal asp.net web app that is used through IE6 has suddenly developed an issue, even though there have been no code changes. Certain images that are being streamed back to the web browser aren't appearing for some users.

I don't know why this was suddenly started happening or what the cause is - however as part of the search, I'm considering if there's a flaw in the code used to stream back images.

The image is held in memory are a byte array, then streamed back using the following code. Is this the best way to stream an image back?

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

// Work out file type
switch( Path.GetExtension( imageFilename ).ToLower() )
{
    case ".jpg":
    case ".jpeg":
        Response.ContentType = "image/jpeg";
        break;
    case ".gif":
        Response.ContentType = "image/gif";
        break;
    default:
        Response.ContentType = "binary/octet-stream";
        break;  
}

Response.AddHeader("Content-Length", imageBytes.Length.ToString() );
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite( imageBytes );
Response.End();

Instead of an image being shown in the page, a red cross is displayed. This only happens with the dynamically generated images shown on the page, rather than the statically linked images.

If I try and open the image in it's own window, I either see the image or a load of gobbledygook text (Which I'm guessing means the MIME type isn't being set/picked up by the browser)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久夏青 2024-11-20 12:20:19

Response.End() 通常是不好的,因为它会中止 IIS 线程,即使它处于 Flush() 中间。

使用 Response.Flush()Response.Close() 确保所有内容都发送到客户端。

Response.End() is generally bad as it aborts the IIS thread even if it's in the middle of Flush()-ing.

Use Response.Flush() followed by Response.Close() to make sure all content is sent to the client.

扬花落满肩 2024-11-20 12:20:19

我对此可能是错误的,但我认为 Content-Length 应该包含正文的长度(以字节为单位)。 Response.BinaryWrite 将对数据进行 base64 编码,该数据将比您在标头中告诉的 byte[] 长度更长。

I could be wrong about this one but i think the Content-Length should contain the length of the body in bytes. Response.BinaryWrite will base64 encode the data wich will be longer then the byte[] length you are telling it is in the header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文