ASP.NET Core流-将块写入请求 [英] ASP.NET Core streaming - write chunks to a request

查看:120
本文介绍了ASP.NET Core流-将块写入请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个已更新的问题,我的代码中曾经有一个错误

我希望能够将数据块发送到客户端.

I would like to be able to send chunks of data over to the client.

任何事情都将不胜感激.

Anything will be appreciated.

有没有一种方法可以为asp.net核心提供更多控制流数据的方式.

Is there a way to provide to asp.net core more control to how it streams the data.

我担心下面的代码如何缩放.

请问有人建议如何通过asp.net核心中的网络api传输数据吗?

Could someone please advise how to go streaming data through a web api in asp.net core?

提供的答案和下面的代码有效.我不确定它如何缩放?

The answer that was provided and the code below works. I am not sure how it scales though?

是否可以检索数据块并将其写入请求,而只需将这些块放入内存即可.这样我就可以下载非常大的文件.

Is it possible to retrieve chunks of data and write them to the request, with only getting the chunks into memory. So i would be able to download very large files.

using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    return File(System.IO.File.OpenRead(filePath), "audio/mpeg");
}

推荐答案

应用FileStream方法-如前所述-使用FileStream

Applying the FileStream approach - as already mentioned - use the FileStream constructor that accepts a bufferSize argument, which specifies the amount of bytes being read into memory.
(You can overrule the default value (4096) to fit your environment.)

public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize);

bufferSize :
大于0的System.Int32的正值表示 缓冲区大小.
默认缓冲区大小为4096.

bufferSize:
A positive System.Int32 value greater than 0 indicating the buffer size.
The default buffer size is 4096.

public IActionResult GetFile()
{ 
    var filePath = @"c:\temp\file.mpg"; // Your path to the audio file.
    var bufferSize = 1024;
    var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize);            
    return File(fileStream, "audio/mpeg");
}

请注意,无需处置fileStreamFile方法会解决这个问题.

Note that there's no need to dispose the fileStream; the File method takes care of this.

要澄清:

传入FileStream时,将按块读取其内容(与配置的缓冲区大小匹配).
具体来说,这意味着它的Read方法(int Read (byte[] array, int offset, int count))会重复执行,直到读取完所有字节为止,从而确保在内存中存储的字节数不超过给定的数量.

When passing in a FileStream, its content is being read in chunks (matching the configured buffersize).
Concrete, this means that its Read method (int Read (byte[] array, int offset, int count)) gets executed repeatedly untill all bytes have been read, ensuring that no more than the given number of bytes are stored in memory.

因此,可扩展性在较少的内存使用量之内,因为如果文件的大小很大,尤其是结合(此文件或其他文件的)高读取频率,内存是一种资源,那么资源可能会承受压力. 这可能会导致内存不足的问题.

So the scalability is within the less memory usage, as memory is a resource that can come under pressure if the size of the file is high, especially in combination with a high read frequency (of this or of other files) which might cause out of memory problems.

这篇关于ASP.NET Core流-将块写入请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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