Sco*_*son 5 c# asp.net-mvc fileresult
我试图通过控制器ActionResult返回大文件,并实现了如下自定义FileResult类.
public class StreamedFileResult : FileResult
{
private string _FilePath;
public StreamedFileResult(string filePath, string contentType)
: base(contentType)
{
_FilePath = filePath;
}
protected override void WriteFile(System.Web.HttpResponseBase response)
{
using (FileStream fs = new FileStream(_FilePath, FileMode.Open, FileAccess.Read))
{
int bufferLength = 65536;
byte[] buffer = new byte[bufferLength];
int bytesRead = 0;
while (true)
{
bytesRead = fs.Read(buffer, 0, bufferLength);
if (bytesRead == 0)
{
break;
}
response.OutputStream.Write(buffer, 0, bytesRead);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我遇到的问题是整个文件似乎被缓冲到内存中.我需要做些什么来防止这种情况发生?
您需要刷新响应以防止缓冲.但是,如果您在不设置内容长度的情况下继续缓冲,则用户将看不到任何进度.因此,为了让用户看到正确的进度,IIS缓冲整个内容,计算内容长度,应用压缩然后发送响应.我们采用以下程序将文件传递给客户端,具有高性能.
FileInfo path = new FileInfo(filePath);
// user will not see a progress if content-length is not specified
response.AddHeader("Content-Length", path.Length.ToString());
response.Flush();// do not add anymore headers after this...
byte[] buffer = new byte[ 4 * 1024 ]; // 4kb is a good for network chunk
using(FileStream fs = path.OpenRead()){
int count = 0;
while( (count = fs.Read(buffer,0,buffer.Length)) >0 ){
if(!response.IsClientConnected)
{
// network connection broke for some reason..
break;
}
response.OutputStream.Write(buffer,0,count);
response.Flush(); // this will prevent buffering...
}
}
Run Code Online (Sandbox Code Playgroud)
您可以更改缓冲区大小,但4kb是理想的,因为较低级别的文件系统也会读取4kb块中的缓冲区.
| 归档时间: |
|
| 查看次数: |
1948 次 |
| 最近记录: |