gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:38 25 4
gpt4 key购买 nike

由于某些奇怪的原因,一个通过 IE6 使用的已有 5 年历史的内部 asp.net web 应用程序 突然出现问题,即使没有代码变化。正在流式传输回 Web 浏览器的某些图像不会对某些用户显示。

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

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

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 类型没有被浏览器设置/拾取)

最佳答案

Response.End() 通常很糟糕,因为它会中止 IIS 线程,即使它正在 Flush()-ing 中也是如此。

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

关于c# - 如何流式传输字节数组图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6252086/

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