如何在.Net MVC中将文件从磁盘流式传输到客户端浏览器 [英] How to stream file from disk to client browser in .Net MVC

查看:374
本文介绍了如何在.Net MVC中将文件从磁盘流式传输到客户端浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的操作将文件从磁盘返回到客户端浏览器,目前我有:

My action returns a file from disk to client browser and currently I have:

public FileResult MediaDownload ()
{
  byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(filePath));
  return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

这样它会将整个文件加载到内存中并且非常慢,因为下载文件加载到内存后启动。处理此类文件下载的最佳方法是什么?

This way it loads whole file in memory and is very slow, as the download start after the file is loaded to memory. What's the best way to handle such file downloads?

谢谢

推荐答案

<好的,我遇到了这个论坛的讨论: http://forums.asp.net/t/ 1408527.aspx

像魅力一样,正如我所需要的那样!

Works like a charm, exactly what I need!

UPDATE

遇到这个问题如何在ASP.NET响应中提供大文件?,事实证明它更简单,我现在就是这样做的:

Came across this question How to deliver big files in ASP.NET Response? and it turns out it's much simpler, here is how I do it now:

var length = new System.IO.FileInfo(Server.MapPath(filePath)).Length;
Response.BufferOutput = false;
Response.AddHeader("Content-Length", length.ToString());
return File(Server.MapPath(filePath), System.Net.Mime.MediaTypeNames.Application.Octet, fileName);

这篇关于如何在.Net MVC中将文件从磁盘流式传输到客户端浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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