ASP.NET MVC4大文件下载的问题 60
项目使用的框架是MVC4,用的FileStreamResult以流的方式返回文件到客户端浏览器,但是出现了2个问题:1、这个方式会在流读取完成之后再将整个文件流返回到客户...
项目使用的框架是MVC4,用的FileStreamResult以流的方式返回文件到客户端浏览器,但是出现了2个问题:1、这个方式会在流读取完成之后再将整个文件流返回到客户端,也就是说文件稍微大点的话点击了下载会等很久才弹出保存窗口;
2、在下载大文件(上百兆的文件)的时候程序会报出一个内存超出的错,
引发类型为“System.OutOfMemoryException”的异常。
下面贴出Controller类中的主要代码:
public FileStreamResult FileDownloadByStream()
{
string path = Request["Path"].ToString(); //保存路径
string fileName = Request["FileName"].ToString(); //文件名
FtpHelper ftp = new FtpHelper(ConfigurationManager.AppSettings["FTPServerIP"],
path,
ConfigurationManager.AppSettings["FTPUserID"],
ConfigurationManager.AppSettings["FTPPassword"]);
FtpWebResponse response = ftp.Open(new Uri(ftp.ftpURI + fileName), WebRequestMethods.Ftp.DownloadFile);
Stream ftpStream = response.GetResponseStream();
return File(ftpStream, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
流是从FTP服务器上获取的,System.Net.FtpDataStream,获取流的长度会报错 展开
2、在下载大文件(上百兆的文件)的时候程序会报出一个内存超出的错,
引发类型为“System.OutOfMemoryException”的异常。
下面贴出Controller类中的主要代码:
public FileStreamResult FileDownloadByStream()
{
string path = Request["Path"].ToString(); //保存路径
string fileName = Request["FileName"].ToString(); //文件名
FtpHelper ftp = new FtpHelper(ConfigurationManager.AppSettings["FTPServerIP"],
path,
ConfigurationManager.AppSettings["FTPUserID"],
ConfigurationManager.AppSettings["FTPPassword"]);
FtpWebResponse response = ftp.Open(new Uri(ftp.ftpURI + fileName), WebRequestMethods.Ftp.DownloadFile);
Stream ftpStream = response.GetResponseStream();
return File(ftpStream, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
流是从FTP服务器上获取的,System.Net.FtpDataStream,获取流的长度会报错 展开
2个回答
展开全部
刚碰到这个问题,下面的代码可以直接拷贝使用。
protected void Page_Load(object sender, EventArgs e)
{
DownFile1(@"D:\常用软件\win7.iso", "win7.iso");
}
private void DownFile1(string filePath, string fileName)
{
ResponseFile(this.Request, this.Response, fileName, filePath, 1024000);
}
// 输出硬盘文件,提供下载
// 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数
// 返回是否成功
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
protected void Page_Load(object sender, EventArgs e)
{
DownFile1(@"D:\常用软件\win7.iso", "win7.iso");
}
private void DownFile1(string filePath, string fileName)
{
ResponseFile(this.Request, this.Response, fileName, filePath, 1024000);
}
// 输出硬盘文件,提供下载
// 输入参数 _Request: Page.Request对象, _Response: Page.Response对象, _fileName: 下载文件名, _fullPath: 带文件名下载路径, _speed 每秒允许下载的字节数
// 返回是否成功
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是ASP.NET webform中的代码,希望对你有用:
string fileName = lb_file.Text;//客户端保存的文件名
string filePath = Server.MapPath("~/Report/" + fileName);//路径
if (!File.Exists(filePath))
{
MessageBox.Show(this, "文件已经删除!无法下载!"); return;
}
//以字符流的形式下载文件
var fs = new FileStream(filePath, FileMode.Open);
var bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询