当前位置: 移动技术网 > IT编程>开发语言>.net > C# Net MVC 大文件下载几种方式、支持速度限制、资源占用小

C# Net MVC 大文件下载几种方式、支持速度限制、资源占用小

2019年02月23日  | 移动技术网IT编程  | 我要评论

新金瓶梅上映时间,n0717,i believe 歌词

上一篇我们说到大文件的分片下载、断点续传、秒传,有的博友就想看分片下载,我们也来总结一下下载的几种方式,写的比较片面,大家见谅^_^。


下载方式:

1、html超链接下载;

2、后台下载(四种方法:返回filestream、返回file、transmittile方法、response分块下载)。


 

1、html超链接下载

  超级链接在本质上属于一个网页的一部分,它是一种允许我们同其他网页或站点之间进行连接的元素。

各个网页链接在一起后,才能真正构成一个网站。

  所谓的超链接是指从一个网页指向一个目标的连接关系,这个目标可以是另一个网页,也可以是相同网页

上的不同位置,还可以是一个图片,一个电子邮件地址,一个文件,甚至是一个应用程序。

  超链接的种类(一般有四种:http,file,ftp,maito):

  1. http          如:<a href="http://www.baidu.com">百度</a>

  2. file           如:<a href="file://images/1.jpg">图片</a>

  3. ftp            如:<a href="ftp://192.168.0.0/">进入</a>

  4. mailto      如:<a href="mailto:qq@163.com">e-mail</a>


 

2、后台下载(四种方法:返回filestream、返回file、transmittile方法、response分块下载)

前台请求后台,后台做出响应进行数据下载。至于请求方式可以多样,比如:a标签跳转,ajax请求等均可。

我们来看后台响应的四种方式:


 

1、返回filestream

    /// <summary>
        /// 返回filestream
        /// </summary>
        /// <returns></returns>
        public filestreamresult filestream_download()
      {
        string filename = "wenjian.txt";//客户端保存的文件名
          string filepath = server.mappath("/upload/wenjian.txt");//指定文件所在的全路径
          return file(new filestream(filepath, filemode.open), "text/plain",//"text/plain"是文件mime类型
        filename);
      }

 


2、返回file

    /// <summary>
        /// 返回file
        /// </summary>
        /// <returns></returns>
        public fileresult file_download()
        {
            string filepath = server.mappath("/upload/wenjian.txt");//路径
            return file(filepath, "text/plain", "wenjian.txt"); //"text/plain"是文件mime类型,welcome.txt是客户端保存的名字
        }

 


3、transmitfile方法

/// <summary>
/// transmitfile方法
/// </summary>

public bool transmitfile_download()
  {
     string filename = "wenjian.txt";//客户端保存的文件名
     string filepath = server.mappath("/upload/wenjian.txt");//路径
     fileinfo fileinfo = new fileinfo(filepath);
     response.clear();         //清除缓冲区流中的所有内容输出
     response.clearcontent();  //清除缓冲区流中的所有内容输出
     response.clearheaders();  //清除缓冲区流中的所有头
     response.buffer = true;   //该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
     response.addheader("content-disposition", "attachment;filename=" + filename);
     response.addheader("content-length",fileinfo.length.tostring());
     response.addheader("content-transfer-encoding", "binary");
     response.contenttype = "application/unknow";  //获取或设置输出流的 http mime 类型
     response.contentencoding = system.text.encoding.getencoding("gb2312"); //获取或设置输出流的 http 字符集
     response.transmitfile(filepath);
     response.end();
   return true; }

 


4、response分块下载

     /// <summary>
        ///  response分块下载,输出硬盘文件,提供下载 支持大文件、续传、速度限制、资源占用小
        /// </summary>
        /// <param name="filename">客户端保存的文件名</param>
        /// <param name="filepath">客户端保存的文件路径(包括文件名)</param>
        /// <returns></returns>
        public bool responsedownload(string filename, string filepath)
        {
            
           filename = "wenjian.txt";//客户端保存的文件名
           filepath = server.mappath("/upload/wenjian.txt"); //路径(后续从webconfig读取)

           system.io.fileinfo fileinfo = new system.io.fileinfo(filepath);
           if (fileinfo.exists == true)
           {
               const long chunksize = 102400;//100k 每次读取文件,只读取100k,这样可以缓解服务器的压力
               byte[] buffer = new byte[chunksize];

               response.clear();
               system.io.filestream istream = system.io.file.openread(filepath);
               long datalengthtoread = istream.length;//获取下载的文件总大小
               response.contenttype = "application/octet-stream";
               response.addheader("content-disposition", "attachment; filename=" + httputility.urlencode(filename));
               while (datalengthtoread > 0 && response.isclientconnected)
               {
                   int lengthread = istream.read(buffer, 0, convert.toint32(chunksize));//读取的大小
                   response.outputstream.write(buffer, 0, lengthread);
                   response.flush();
                   datalengthtoread = datalengthtoread - lengthread;
               }
               response.close();
               return true;
           }
           else
               return false;
        }

 


 

总结:

以上就是我所了解的几种下载方式,个人比较中意response分块下载。

感谢大家的支持,写的不好望大家见谅。^_^

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网