当前位置: 移动技术网 > IT编程>开发语言>JavaScript > web下载附件及修改名称

web下载附件及修改名称

2019年11月14日  | 移动技术网IT编程  | 我要评论
 1 /**
 2  * @param: url 附件地址
 3  * @param: filename 下载后的文件名
 4  */
 5 function download(url, filename) {
 6     getblob(url, function (blob) {
 7         saveas(blob, filename);
 8     });
 9 
10 }
11 
12 function getblob(url, cb) {
13 
14     var xhr = new xmlhttprequest();
15 
16     xhr.open('get', url, true);
17 
18     xhr.responsetype = 'blob';
19 
20     xhr.onload = function () {
21 
22         if (xhr.status === 200) {
23 
24             cb(xhr.response);
25 
26         }
27 
28     };
29 
30     xhr.send();
31 
32 }
33 
34 function saveas(blob, filename) {
35 
36     if (window.navigator.mssaveoropenblob) {
37 
38         navigator.mssaveblob(blob, filename);
39 
40     } else {
41 
42         var link = document.createelement('a');
43 
44         var body = document.queryselector('body');
45 
46         link.href = window.url.createobjecturl(blob);
47 
48         link.download = filename;
49 
50         // fix firefox
51 
52         link.style.display = 'none';
53 
54         body.appendchild(link);
55 
56         link.click();
57 
58         body.removechild(link);
59 
60         window.url.revokeobjecturl(link.href);
61 
62     };
63 
64 }

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网