下载图片到本地,多个图片压缩后下载到本地

单个图片的下载:
第一种

async downLoadImage() {
   
    let response = await fetch('https://img-home.csdnimg.cn/images/20231127111739.png');
    let data = await response.blob();
    let url = window.URL.createObjectURL(data);
    let link = document.createElement('a');
    link.href = url;
    link.download = 'image.jpg';
    link.click();
}

第二种,支持旧浏览器

downloadImage() {
   
  const imageUrl = 'https://img-home.csdnimg.cn/images/20231127111739.png';
  const xhr = new XMLHttpRequest();
  xhr.open("GET", imageUrl, true);
  xhr.responseType = "blob";
  xhr.onload = function () {
   
    if (xhr.status === 200) {
   
      const url = window.URL.createObjectURL(xhr.response);
      const link = document.createElement("a");
      link.href = url;
      link.download = "image22222.png";
      link.click();
    } else {
   
      console.error("图片下载失败");
    }
  };
  xhr.send();
}

多张图片的下载,需要压缩
下面是用的vue2技术:

import JSZip from "jszip";
import {
    saveAs } from "file-saver";
import axios from 'axios'

  // 下载文件
function getFile (url) {
   
  return new Promise((resolve, reject) => {
    axios2.get(url, { responseType: 'arraybuffer' })
      .then(response => {
        resolve(response.data)
      })
      .catch(error => {
        console.log(`Failed to download ${url}`, error);
        return null; // return null when request fails
      })
  })
}

export function downFileAll(images, packName) {
      const zip = new JSZip()
      const cache = {}
      const zip = new JSZip()
      const cache = {}
      const promises = images.map(url => getFile(url).then(data => {
        const arr_name = url.split("/");
        let file_name = arr_name[arr_name.length - 1];
        zip.file(file_name, data, {
          binary: true
        });
        cache[file_name] = data;
        return data; 
      }));
    
      return Promise.all(promises).then(() => {
   
        return zip.generateAsync({
    type: "blob" });
      }).then(content => {
   
        saveAs(content, packName);
      });
}

调用方法:
imageUrl1:是图片的url地址
downloadImages([‘imageUrl1’, ‘imageUrl2’, ‘imageUrl3’, …], ‘packName’);

相关推荐

  1. 下载图片本地图片压缩下载本地

    2024-02-21 18:44:02       29 阅读
  2. uniapp微信小程序下载保存图片本地,base64

    2024-02-21 18:44:02       48 阅读
  3. vuePC 录制桌面 并下载本地

    2024-02-21 18:44:02       8 阅读
  4. Python爬虫之保存图片本地

    2024-02-21 18:44:02       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-21 18:44:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-21 18:44:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-21 18:44:02       20 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-21 18:44:02       20 阅读

热门阅读

  1. MySQL中Binlog日志的使用

    2024-02-21 18:44:02       26 阅读
  2. HTML世界之第三重天

    2024-02-21 18:44:02       24 阅读
  3. Docker基本使用【数据卷的挂载及常用命令】

    2024-02-21 18:44:02       30 阅读
  4. 技术应用:C# System.Data.DataTable().Compute 基本用法

    2024-02-21 18:44:02       32 阅读
  5. LLaMA 2 - 你所需要的一切资源

    2024-02-21 18:44:02       27 阅读
  6. 【达梦数据库】通过系统函数来配置sqllog

    2024-02-21 18:44:02       26 阅读
  7. 设计模式的另一种有趣理解

    2024-02-21 18:44:02       26 阅读
  8. HTTP常见状态码(持续更新中~~)

    2024-02-21 18:44:02       28 阅读