需要安装jszip,file-saver。俩个npm包
    示例:
    const imgsArr =
    http://imgs.jddmoto.com/carport/goods/4/1584086033256.jpeg!official300?_1242_1242’,
    ‘[http://imgs.jddmoto.com/carport/goods/4/1584086042913.jpeg!official300?_640_395‘]
    pictureDownload(imgsArr)

    1. import JSZip from 'jszip'
    2. import FileSaver from 'file-saver'
    3. // that就是this
    4. import that from '@/main.js's
    5. export function pictureDownload(data) {
    6. that.$message.success('正在下载...')
    7. download(data)
    8. }
    9. async function download(data) {
    10. try {
    11. let zip = new JSZip()
    12. for (let i = 0; i < data.length; i++) {
    13. let lst = data[i].split('.')
    14. let fileType = lst[lst.length - 1]
    15. let fileName = data[i].split('.').reverse()[1].split('/').reverse()[0]
    16. let fileFormat = data[i].split('.').reverse()[0].split('!')[0]
    17. if (fileType.toLocaleUpperCase() === 'PDF') {
    18. await getFile(data[i]).then(pdf => {
    19. zip.file('HaLuo.pdf', pdf, {
    20. binary: true
    21. })
    22. })
    23. } else {
    24. await getBase64Image(data[i]).then(res => {
    25. zip.file(fileName + `.${fileFormat}`, res, {
    26. base64: true
    27. })
    28. })
    29. }
    30. }
    31. downImg(zip)
    32. } catch (err) {
    33. console.log('err', err)
    34. }
    35. }
    36. function downImg(zip) {
    37. zip
    38. .generateAsync({
    39. type: 'blob'
    40. })
    41. .then(content => {
    42. let fileName = '批量下载.zip'
    43. FileSaver.saveAs(content, fileName)
    44. })
    45. }
    46. //****传入图片链接,返回base64数据
    47. function getBase64Image(url) {
    48. return new Promise((resolve, reject) => {
    49. var base64 = ''
    50. var img = new Image()
    51. img.useCORS = true; // 解决跨域
    52. img.setAttribute('crossOrigin', 'Anonymous') // 解决跨域
    53. img.onload = () => {
    54. base64 = image2Base64(img)
    55. resolve(base64.split(',')[1])
    56. }
    57. img.onerror = () => reject('加载失败')
    58. // 这里可能会有跨域失败的问题,解决方案同上,url + 随机数
    59. img.src = url + '?=' + Math.random()
    60. })
    61. }
    62. function image2Base64(img) {
    63. var canvas = document.createElement('canvas')
    64. canvas.width = img.width
    65. canvas.height = img.height
    66. var ctx = canvas.getContext('2d')
    67. ctx.drawImage(img, 0, 0, img.width, img.height)
    68. var dataURL = canvas.toDataURL('image/png')
    69. return dataURL
    70. }
    71. //****传入文件链接,返回arraybuffer数据
    72. function getFile(url) {
    73. return new Promise((resolve, reject) => {
    74. that
    75. .$http({
    76. method: 'get',
    77. url,
    78. responseType: 'arraybuffer'
    79. })
    80. .then(data => {
    81. resolve(data.data)
    82. })
    83. .catch(error => {
    84. reject('PDF加载失败:' + error)
    85. })
    86. })
    87. }