C# 使用 ASP.Net Webapi 流式传输大图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14697497/
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
Streaming large images using ASP.Net Webapi
提问by raj
We are trying to return large image files using ASP.Net WebApi and using the following code to stream the bytes to the client.
我们正在尝试使用 ASP.Net WebApi 返回大图像文件,并使用以下代码将字节流式传输到客户端。
public class RetrieveAssetController : ApiController
{
// GET api/retrieveasset/5
public HttpResponseMessage GetAsset(int id)
{
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
string filePath = "SomeImageFile.jpg";
MemoryStream memoryStream = new MemoryStream();
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
memoryStream.Write(bytes, 0, (int)file.Length);
file.Close();
httpResponseMessage.Content = new ByteArrayContent(memoryStream.ToArray());
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
}
The above code works fine but some of the files that we deal with could be 2 GB and upwards in size resulting in connection timeouts. We have used code similar to below in the past (using HttpHandlers) to chunk the response to the response stream to keep the connection alive with success.
上面的代码工作正常,但我们处理的一些文件的大小可能为 2 GB 及以上,从而导致连接超时。过去我们使用过类似于下面的代码(使用 HttpHandlers)将响应分块到响应流,以保持连接成功。
byte[] b = new byte[this.BufferChunkSize];
int byteCountRead = 0;
while ((byteCountRead = stream.Read(b, 0, b.Length)) > 0)
{
if (!response.IsClientConnected) break;
response.OutputStream.Write(b, 0, byteCountRead);
response.Flush();
}
How can we use a similar technique using the new WebAPI programming model shown earlier?
我们如何使用前面显示的新 WebAPI 编程模型使用类似的技术?
采纳答案by Filip W
Yes you can use PushStreamContent
. And if you combine it with asynchronous execution (usin i.e. async lambdas), you might get even more effective results.
是的,您可以使用PushStreamContent
. 如果你将它与异步执行结合起来(即使用异步 lambdas),你可能会得到更有效的结果。
I have blogged about this approach earlier this month - http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/.
我在本月早些时候写了一篇关于这种方法的博客 - http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/。
The example used a video file, the principle is the same - pushing down bytes of data to the client.
这个例子使用了一个视频文件,原理是一样的——向下推送数据字节到客户端。
回答by drzaus
Stream directly from the file using StreamContent
(too new?). Similar to Web API Controller convert MemoryStream into StreamContent
使用StreamContent
(太新?)直接从文件流式传输。类似于Web API Controller 将 MemoryStream 转换为 StreamContent
httpResponseMessage.Content = new StreamContent(file);