ali_oss.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const AliOss = (function (){
  2. const downloadFile = function (url, filename) {
  3. axios.get(url, {responseType: 'blob' }).then(res => {
  4. saveAs(res.data, filename);
  5. });
  6. };
  7. const downloadFileSync = function(url) {
  8. return new Promise((resolve, reject) => {
  9. axios.get(url, {responseType: 'blob'}).then(res => {
  10. resolve(res.data);
  11. }).catch(err => {
  12. reject(err);
  13. });
  14. })
  15. };
  16. const zipFiles = function (files, filename = '打包.zip', successCallback, errorCallback) {
  17. const zip = new JSZip();
  18. const download = [], fails = [];
  19. files.forEach(f => {
  20. download.push(downloadFileSync(f.filepath).then(data => {
  21. zip.file(f.filename + f.fileext, data, {binary: true});
  22. }).catch(err => {
  23. fails.push(f);
  24. }));
  25. });
  26. Promise.all(download).then(() => {
  27. if (fails.length < files.length) {
  28. zip.generateAsync({ type: "blob" }).then(content => {
  29. saveAs(content, filename);
  30. successCallback && successCallback(fails);
  31. });
  32. } else {
  33. errorCallback && errorCallback(fails);
  34. }
  35. })
  36. };
  37. return { downloadFile, downloadFileSync, zipFiles}
  38. })();