如何流字节数组形象? [英] How to stream byte array image?

查看:96
本文介绍了如何流字节数组形象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关一些奇怪的原因,一个五岁内部的 asp.net web应用程序通过IE6使用突然开发了一个问题,即使没有出现过code钮。正在源源不断地送回Web浏览器的某些图像不会针对某些用户。

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.

我不知道为什么会这样突然开始发生或原因是什么 - 但是作为搜索的一部分,我考虑是否有在code一大败笔用于流影像

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.

图片保存在内存中的一个字节数组,然后流回使用以下code。这是流图像回的最佳方式?

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.

如果我试图在它自己的窗口中打开图像,我要么看到的图像或文字官样文章的负载(我猜是指MIME类型不被设置/拾取的浏览器)

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)

推荐答案

到Response.End()一般是糟糕,因为它会中止,即使它在中东的IIS线程同花顺() - ING。

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

使用 Response.Flush()然后按 Response.Close(),以确保所有的内容发送到客户端

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

这篇关于如何流字节数组形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆