使用原生js、XMLHttpRequest实现下载文件(三种方式)

一、使用XMLHttpRequest,获取response 实现下载
(1)post

   				let that = this;
                let params = {
                    taskId: that.fieId
                };
                let xhr = new XMLHttpRequest();
                xhr.open('POST', '/ids/fap/session/pcap-export-download', true);
                xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
                xhr.responseType = 'blob';
                xhr.onload = function (e) {
                    if (this.status == 200) {
                        let blob = this.response;
                        let a = document.createElement('a');
                        let url = window.URL.createObjectURL(blob);
                        a.href = url;
                        //获取后端文件名称
                        let fileName = decodeURI(xhr.getResponseHeader('content-disposition'));
                        console.log(fileName)//attachment;filename=日志文件20201211092152.zip
                        //截取=字符串后面的内容
                        let str = fileName.match(/=(\S*)/)[1];
                        console.log(str)
                        a.download = str;
                        a.click();
                        window.URL.revokeObjectURL(url);
                    }
                };
                //参数是json格式
                xhr.send(JSON.stringify(params));

(2) get

  		  let that = this;
          let urlData = '/ids/report/exportWord' + "?startTime=" + that.report.startTime + "&endTime=" + that.report.endTime +"&type="+type
          let xhr = new XMLHttpRequest();
          xhr.open('GET', urlData, true);
          xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
          xhr.responseType = 'blob';
          xhr.onload = function (e) {
            if (this.status == 200) {
              let blob = this.response;
              let a = document.createElement('a');
              let url = window.URL.createObjectURL(blob);
              a.href = url;
              let time = Util.timestampToTime(new Date(), 'YYYY-MM-DD HH_MM_SS')
              a.download = name+time + '.doc';
              a.click();
              window.URL.revokeObjectURL(url);
            }
          };
          xhr.send();

二、 构造form表单,form.submit() ,仅仅支持formData的参数格式

 					let form = document.createElement("form");
                    form.style.display = 'none';
                    form.action = '/hmonitor-web/warn/event/export';
                    form.method = "post";
                    document.body.appendChild(form);
                    //that.queryForm 参数
                    let objs = that.$.extend(true, {}, that.queryForm);
                    delete objs.currentPage;
                    delete objs.pageSize;
                    for (let key in objs) {
                        let input = document.createElement("input");
                        input.type = "hidden";
                        input.name = key;
                        input.value = objs[key];
                        form.appendChild(input)
                    }
                    form.submit();
                    form.remove()

三、简单的get请求

window.location.href = ‘./download’ + “?remote=” +this.remote;

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值