当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.netMVC文件下载的几种方法

ASP.netMVC文件下载的几种方法

2018年10月12日  | 移动技术网IT编程  | 我要评论

求助吧,8l9868,东辽县教育信息网

第一种:最简单的超链接方法,标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了

第二种:后台下载

 

在后台下载中又可以细分为几种下载方式

首先,在前台,我们需要一个标签

"~/home/download">click to get file

home为controller,download为action。

如果需要传一些参数,可以:

"~/home/download?id=1">click to get file

 

在后台:

(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 }
复制代码

 

+ajax方法

要重点说说这个方法,ajax返回不了文件流,所以说用ajax调用上面任意一种后台方法都要出问题,下载不了文件。

所以,只能让后台返回所需下载文件的url地址,然后调用windows.location.href。

 

优点:ajax可以传好几个参数(当然以json形式),传100个都无所谓。你要是用的方法传100得写死。。。(公司需求,至少要传100多个参数)

缺点:支持下载exe,rar,msi等类型文件。对于txt则会直接打开,慎用!对于其他不常用的类型文件则直接报错。

        所以我的建议是得到多个参数,通过数据库找到所有需要下载的文件然后压缩打包,然后返回url下载。(你要是一个一个给用户下,用户都疯了)

        那么问题又来了,c#怎么用代码实现将本地的一些文件打包压缩到服务器目录下呢?这我不知道。

           因为你只是单纯的放在目录文件夹下没有用的,我们平时在服务器某目录下添加某一个文件都是右键,添加xxx项这样,这样才能真正的将文件放在服务器中。

           可是纯代码该怎么实现呢??

        * 可能的解决方法:先在项目目录下放一个空的rar文件或者没什么功能的exe,msi文件,然后在后台打包完一些文件后去替换它,不知道可行不。

            (1)首先清空原压缩包中的内容

            (2)将文件压缩到压缩包中

            (3)返回 xxx.rar

 

前端:

"button" id="downloaon"/>

ajax:

复制代码
$("#downloaon").click(function () {
                $.ajax({
                    type: "get",
                    url: "/home/download",
                    data: { id: "1" },
                    datatype:"json",
                    success: function (result) {
                        window.location.target = "_blank";
                        window.location.href = result;                      
                    }
                })
            })
复制代码

后台:

public string download(int id)
{
            string filepath = "document/123.msi";//路径
            return filepath;
}

这里,id是没有什么作用的,后台就别用什么server.mappath了,肯定错。直接写服务器项目文件夹/文件名 即可。

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

相关文章:

验证码:
移动技术网