vue2 jszip + file-saver下载图片zip

今天做项目的时候一个功能是需要批量下载图片到本地在网上查了一些资料不是很全所以记录一下

首先npm安装引入jszip

npm install jszip file-saver --save
import JSZip from 'jszip';
import { saveAs } from 'file-saver';

然后上方法

// 下载图片zip
    async downloadImages() {
      try {
        // 创建一个新的JSZip实例
        const zip = new JSZip();
        const imageUrls = this.urls1; 
        //this.urls1: 这是图片的数组,如果是本地图片的话需要require()方法包裹一下

        // 用于追踪已处理的图片数量
        let processedImages = 0;

        const fetchImages = async () => {
          if (processedImages < imageUrls.length) {
            const url = imageUrls[processedImages];
            const response = await fetch(url);
            const blob = await response.blob();

            // 将图片添加到JSZip实例中
            zip.file(`image${processedImages + 1}.${url.split('.').pop()}`, blob, { binary: true });

            processedImages++;

            // 递归调用以处理下一张图片
            fetchImages();
          } else {
            // 所有图片都已处理,生成ZIP文件
            zip.generateAsync({ type: 'blob' })
              .then(zipBlob => {
                // 使用FileSaver的saveAs方法下载ZIP文件
                saveAs(zipBlob, 'images.zip'); // images.zip 为下载的压缩包名
              })
              .catch(error => {
                console.error('Error generating ZIP blob:', error);
              });
          }
        };

        // 开始处理图片URL数组
        fetchImages();
      } catch (error) {
        console.error('Error downloading images:', error);
      }
    },

记录完毕

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对于动态生成路由,你可以使用 Vue Router 的 addRoutes 方法动态添加路由。具体实现可以参考以下代码: ```typescript // 路由配置 const routes: RouteRecordRaw[] = [ { path: '/login', component: () => import('@/views/Login.vue') }, { path: '/', component: () => import('@/views/Home.vue'), children: [ { path: '/dashboard', component: () => import('@/views/Dashboard.vue') } // 其他路由 ] } ] // 创建 router 实例 const router = createRouter({ history: createWebHashHistory(), routes }) // 在登录成功后动态添加路由 router.addRoute({ path: '/user', component: () => import('@/views/User.vue') }) ``` 对于模块化管理,你可以将页面和组件按照功能模块拆分到不同的文件夹中,并使用 Vue 的异步组件实现按需加载。如下所示: ```vue <!-- 在路由配置文件中 --> { path: '/dashboard', component: () => import('@/views/dashboard/Dashboard.vue') } <!-- Dashboard.vue --> <template> <!-- Dashboard 页面内容 --> </template> <script> export default { // ... } </script> <style scoped> /* Dashboard 页面样式 */ </style> ``` 对于国际化,你可以使用 Vue I18n 插件来实现。首先需要安装和配置该插件,然后在页面中使用 `$t` 方法来访问翻译文本,如下所示: ```vue <!-- 在组件中 --> <template> <div> {{ $t('hello') }} </div> </template> <script> export default { // ... } </script> ``` 对于换肤,你可以使用 CSS 变量来实现。首先在全局样式表中定义一些颜色变量,然后在页面中使用这些变量,如下所示: ```css /* 在全局样式表中 */ :root { --primary-color: #42b983; --secondary-color: #ff7f50; } /* 在组件样式表中 */ .button { background-color: var(--primary-color); color: white; } .button:hover { background-color: var(--secondary-color); } ``` 对于打包 zip 下载,你可以使用 JSZip 库来实现。具体实现可以参考以下代码: ```typescript import JSZip from 'jszip' import FileSaver from 'file-saver' // 创建 zip 文件 const zip = new JSZip() // 将需要打包的文件添加到 zip 文件中 zip.file('index.html', '<html><body>Hello World!</body></html>') // 生成 zip 文件并下载 zip.generateAsync({ type: 'blob' }).then((content) => { FileSaver.saveAs(content, 'example.zip') }) ``` 以上是使用 Vue3 + TypeScript + Element Plus + Axios + Router + Pinia + Vite 实现动态生成路由、模块化管理、国际化、换肤和打包 zip 下载的简要方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值