| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import Common from './Common.js';
- import Notify from './Notify.js';
- /**
- * 报表下载服务
- */
- export default {
- /**
- * GET 方式下载文件
- * @param {*} url
- * @param {*} fileName
- */
- downloadFile: function (url, fileName) {
- fetch(url, {
- method: 'GET',
- headers: {
- 'token': localStorage.getItem('#token'),
- },
- })
- .then(res => res.blob())
- .then(data => {
- const blobUrl = window.URL.createObjectURL(data);
- var a = document.createElement('a');
- a.download = fileName;
- a.href = blobUrl;
- $('body').append(a); // 修复firefox中无法触发click
- a.click();
- $(a).remove();
- }).catch(err => {
- Notify.error(err.code, err.responseText, true);
- });
- },
- /**
- * 报表下载
- * @param {Object} fileName
- * @author GuoZhiBo 20200410
- */
- reportDownload: function (fileName) {
- var downloadUrl = Common.getApiURL('file/reportDownload') + '?fileName=' + window.encodeURIComponent(fileName);
- this.downloadFile(downloadUrl, fileName);
- },
- /**
- * 文件下载
- * @param {Object} className 类名称
- * @param {Object} fileName 文件名称
- * @author GuoZhiBo 20211008
- */
- fileDownload:function(className, fileName){
- var downloadUrl = Common.getApiURL('file/fileDownload') + '?className=' + className
- + '&fileName=' + window.encodeURIComponent(fileName);
- this.downloadFile(downloadUrl, fileName);
- },
- /**
- * POST 方式下载文件
- * @param {http请求的地址} url
- * @param {post请求需要的参数} params
- */
- postDownloadFile: function (url, params) {
- var form = document.createElement('form');
- form.style.display = 'none';
- form.action = url;
- form.method = 'post';
- document.body.appendChild(form);
- // 动态创建input并给value赋值
- for (var key in params) {
- var input = document.createElement('input');
- input.type = 'hidden';
- input.name = key;
- input.value = params[key];
- form.appendChild(input);
- }
- form.submit();
- form.remove();
- },
- };
|