12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- const AliOss = (function (){
- const setting = {
- hint: true
- };
- const downloadFile = function (url, filename) {
- axios.get(url, {responseType: 'blob' }).then(res => {
- saveAs(res.data, filename);
- });
- };
- const downloadFileSync = function(url) {
- return new Promise((resolve, reject) => {
- axios.get(url, {responseType: 'blob'}).then(res => {
- resolve(res.data);
- }).catch(err => {
- reject(err);
- });
- })
- };
- const zipFiles = function (files, filename = '打包.zip', successCallback, errorCallback) {
- const zip = new JSZip();
- const download = [], fails = [];
- files.forEach(f => {
- download.push(downloadFileSync(f.filepath).then(data => {
- if (setting.hint) {
- toastr.success(`文件 “${f.filename + f.fileext}” 下载成功...`);
- }
- zip.file(f.filename + f.fileext, data, {binary: true});
- }).catch(err => {
- fails.push(f);
- }));
- });
- Promise.all(download).then(() => {
- toastr.clear();
- if (fails.length < files.length) {
- if (setting.hint) {
- toastr.success('所有文件下载成功,压缩中...');
- }
- zip.generateAsync({ type: "blob" }).then(content => {
- saveAs(content, filename);
- successCallback && successCallback(fails);
- });
- } else {
- errorCallback && errorCallback(fails);
- }
- })
- };
- const setSetting = function(data) {
- if (!data) return;
- setting.hint = data.hint || setting.hint;
- };
- return { downloadFile, downloadFileSync, zipFiles, setSetting }
- })();
|