关闭

ASP.Net MVC 文件下载的几种方法

发表于:2016-5-18 10:34

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:airforce094    来源:51Testing软件测试网采编

  在ASP.net MVC 中有几种下载文件的方法
  第一种:最简单的超链接方法,<a>标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了
  第二种:后台下载
  在后台下载中又可以细分为几种下载方式
  首先,在前台,我们需要一个<a>标签
  <a href="~/Home/download">Click to get file</a>
  Home为controller,download为action。
  如果需要传一些参数,可以:
  <a href="~/Home/download?id=1">Click to get file</a>
  在后台:
  (1)返回filestream
  public FileStreamResult download()
  {
  string fileName = "aaa.txt";//客户端保存的文件名
  string filePath = Server.MapPath("~/Document/123.txt");//路径
  return File(new FileStream(filePath, FileMode.Open), "text/plain",
  fileName);
  }
  其中:“text/plain”是文件MIME类型
  (2)返回file
  public FileResult download()
  {
  string filePath = Server.MapPath("~/Document/123.txt");//路径
  return File(filePath, "text/plain", "welcome.txt"); //welcome.txt是客户端保存的名字
  }
  (3)TransmitFile方法
1  public void download()
2 {
3       string fileName = "aaa.txt";//客户端保存的文件名
4       string filePath = Server.MapPath("~/Document/123.txt");//路径
5       FileInfo fileinfo = new FileInfo(filePath);
6             Response.Clear();         //清除缓冲区流中的所有内容输出
7             Response.ClearContent();  //清除缓冲区流中的所有内容输出
8             Response.ClearHeaders();  //清除缓冲区流中的所有头
9             Response.Buffer = true;   //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
10             Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
11             Response.AddHeader("Content-Length",fileinfo.Length.ToString());
12             Response.AddHeader("Content-Transfer-Encoding", "binary");
13             Response.ContentType = "application/unknow";  //获取或设置输出流的 HTTP MIME 类型
14             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //获取或设置输出流的 HTTP 字符集
15             Response.TransmitFile(filePath);
16             Response.End();
17 }
  (4)Response分块下载
1 public void download()
2         {
3             string fileName = "aaa.txt";//客户端保存的文件名
4             string filePath = Server.MapPath("~/Document/123.txt");//路径
5
6             System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
7             if (fileInfo.Exists == true)
8             {
9                 const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
10                 byte[] buffer = new byte[ChunkSize];
11
12                 Response.Clear();
13                 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
14                 long dataLengthToRead = iStream.Length;//获取下载的文件总大小
15                 Response.ContentType = "application/octet-stream";
16                 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
17                 while (dataLengthToRead > 0 && Response.IsClientConnected)
18                 {
19                     int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
20                     Response.OutputStream.Write(buffer, 0, lengthRead);
21                     Response.Flush();
22                     dataLengthToRead = dataLengthToRead - lengthRead;
23                 }
24                 Response.Close();
25             }
26         }
  后记:本想用<input>标签调用ajax方法让后台下载文件,发现无法实现,而且其代码量远高于<a>标签,故我就不考虑<input>+ajax。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号