zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { format } from '@/utils';
import { BoleRegExp, FILEMIME } from '@bole-core/core';
import { saveAs } from 'file-saver';
import JSZip from 'jszip';
 
export function downloadFile(data, fileName, fileSuffix) {
  // const name = fileName + '_' + format(new Date(), "yyyyMMddhhmmss") + '.' + fileSuffix
  // saveAs(data, name);
 
  const blob = new Blob([data], { type: FILEMIME[fileSuffix] });
  if ('download' in document.createElement('a')) {
    // 非IE下载
    const elink = document.createElement('a');
    elink.download = fileName + '_' + format(new Date(), 'YYYYMMDDhhmmss') + '.' + fileSuffix; //命名下载名称
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blob); //表示一个指定的file对象或Blob对象
    document.body.appendChild(elink);
    elink.click(); //点击触发下载
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    //navigator.msSaveBlob 废弃;
    // navigator.msSaveBlob(
    //   blob,
    //   fileName + "_" + format(new Date(), "yyyyMMDDhhmmss") + "." + fileSuffix
    // );
    return false;
  }
}
 
export function downloadFileByUrl(fileUrl, fileName?: string) {
  //const suffix = /\.([0-9a-z]+)(?:[\?#]|$)/i.exec(fileUrl)[1];
  const suffix = /\.([0-9a-z]+)(?:[#]|$)/i.exec(fileUrl)[1];
  let name = fileName ? fileName : fileUrl.replace(BoleRegExp.RegFileName, '$2');
  name += `.${suffix}`;
  saveAs(fileUrl, name);
  return;
  // if (/pdf/gi.test(suffix)) {
  //   handlePdfLink(fileUrl, name);
  // } else {
  //   handleFileDownload(fileUrl, name);
  // }
}
export const handleFileSaver = (file, name) => {
  saveAs(file, name);
  console.log(file, name);
};
export function handleFileDownload(url, filename) {
  // 创建 a 标签
  const elink = document.createElement('a'); // 创建a标签
  elink.style.display = 'none';
  elink.download = filename; // 设置下载文件的文件名
  elink.href = url; // content为后台返回的下载地址
  document.body.appendChild(elink);
  elink.click(); // 设置点击事件
  URL.revokeObjectURL(elink.href); // 释放URL 对象
  document.body.removeChild(elink);
}
 
export const getFileByBlob = (url) => {
  return new Promise((resolve, reject) => {
    fetch(url, {
      method: 'get',
      //responseType: "blob"
    })
      .then(function (res) {
        if (res.status !== 200) {
          return res.json();
        }
        return res.blob();
      })
      .then((blobRes) => {
        resolve(blobRes);
      })
      .catch((err) => {
        reject(err);
      });
  });
};
 
export const downloadWithZip = (fileList, zipName, callback) => {
  if (fileList.length === 0) {
    return;
  }
  const zip = new JSZip();
 
  Promise.resolve()
    .then(() => {
      return fileList.reduce((accumulator, fileInfo) => {
        let newName = '';
        if (typeof fileInfo !== 'string') {
          newName = fileInfo.newName;
          fileInfo = fileInfo.url;
        }
 
        // eslint-disable-next-line no-useless-escape
        const suffix = /\.([0-9a-z]+)(?:[\?#]|$)/i.exec(fileInfo)[1];
        let name = newName || fileInfo.replace(BoleRegExp.RegFileName, '$2');
        name += `.${suffix}`;
 
        return accumulator.then(() =>
          fetch(fileInfo)
            .then((resp) => resp.blob())
            .then((blob) => {
              zip.file(name, blob);
            })
        );
      }, Promise.resolve());
    })
    .then(() => {
      zip.generateAsync({ type: 'blob' }).then((content) => {
        // 生成二进制流
        downloadFile(content, zipName, 'zip');
        callback && callback();
      });
    });
};