当前位置: 首页 > news >正文

vue3 下载图片(包括多图片下载)

单图片下载

//使用
download('https://img1.baidu.com/it/u=1493209339,2544178769&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1715101200&t=854f3434686cfd2cba9d6a528597d15c')//下载逻辑
const download = async (modelUrl) => {const response = await fetch(modelUrl);// 将响应体转换为Blobconst blob = await response.blob();// 创建隐藏的a标签来触发下载const link = document.createElement('a');link.href = URL.createObjectURL(blob);link.download = 'image.jpg'; // 自定义下载的文件名link.click();document.body.removeChild(link); // 下载后移除元素
}

多图片打包下载
安装yarn add jszipnpm i jszip
yarn add file-savernpm i file-saver

import JSZip from "jszip"
import { saveAs } from 'file-saver'
const imgList = ref([{ id: 1, url: 'https://img1.baidu.com/it/u=1493209339,2544178769&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1715101200&t=854f3434686cfd2cba9d6a528597d15c' }, { id: 2, url: 'https://img1.baidu.com/it/u=3714509592,1056938510&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1715101200&t=855f002cda64244524d6945999b0be2a' }])
const downloadImg = async () => {const imageUrls = []; // 替换为实际的图片URL数组const filenames = []; // 对应的文件名数组arr.forEach(ii => {imageUrls.push(ii.url)filenames.push((ii.name || ii.id) + '.png')})createAndDownloadImageZip(imageUrls, filenames);
}//
async function createAndDownloadImageZip(images, filenames) {console.log(images)const zip = new JSZip();for (let i = 0; i < images.length; i++) {// 获取图片资源fetch(images[i]).then(response => response.blob()).then(blob => {// 将Blob添加到ZIP包中,这里假设图片名为'image'+i+'.jpg'zip.file(`img${i}.jpg`, blob);// 在所有图片都处理完之后生成ZIPif (i === images.length - 1) {zip.generateAsync({ type: 'blob' }).then(content => {// 下载ZIP文件saveAs(content, 'downloaded_images.zip');});}});}
}
http://www.lryc.cn/news/342407.html

相关文章:

  • LabVIEW如何通过子VI更改主VI控件属性?
  • 关于MS-DOS时代的回忆
  • 数据库索引(Mysql)
  • 异常-Exception
  • ctfshow——SQL注入
  • 第十三章 计算机网络
  • 商品详情 API 返回值说明
  • 层级实例化静态网格体组件:开启大量模型处理之门
  • 【网络知识】光猫、路由器 和 交换机 的作用和区别?
  • 初识Electron,创建桌面应用
  • AI编码时代到来?实现编程梦想的利器—Baidu Comate测评
  • 去中心化自治组织(DAO)
  • MySQL之多表查询
  • 极端天气频发,我们普通人如何保全自己
  • 直面市场乱价,品牌商家该如何解决?
  • Spring中的Bean相关理解
  • 操作系统实战(二)(linux+C语言)
  • 哪些情况下会触发MySQL的预读机制?
  • react使用谷歌人机验证
  • java JMH 学习
  • 本地运行AI大模型简单示例
  • 图像处理:时域、空域、频率的滤波介绍
  • TC8002D 是一颗带关断模式的音频功放IC
  • 深度学习之基于Vgg19预训练卷积神经网络图像风格迁移系统
  • MySQL:多表查询练习
  • # 从浅入深 学习 SpringCloud 微服务架构(八)Sentinel(1)
  • [微信小程序] 入门笔记2-自定义一个显示组件
  • YOLO代码复现
  • 使用fitten code插件(vscode),替换通义千问,识别需求中的输入输出
  • vue使用pdfjs-dist在电脑上展示PDF文件