问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

JS批量下载图片的实现方法详解

创作时间:
作者:
@小白创作中心

JS批量下载图片的实现方法详解

引用
1
来源
1.
https://docs.pingcode.com/baike/2371301

要实现JS批量下载图片,可以使用多个技术手段,包括AJAX请求、Blob对象以及文件下载库。这些方法可以帮助你高效地批量下载图片、处理文件数据以及管理下载任务。下面将详细介绍这些方法,并提供示例代码和实践建议。

一、使用AJAX请求和Blob对象

AJAX请求和Blob对象是实现批量下载图片的基础技术。通过AJAX请求获取图片数据,并使用Blob对象创建下载链接,可以实现高效的图片下载。

1、AJAX请求图片数据

AJAX(Asynchronous JavaScript and XML)请求是从服务器获取数据的常用方法。可以使用XMLHttpRequest对象或fetchAPI来发送AJAX请求。

async function fetchImage(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  return blob;
}

2、创建Blob对象

Blob对象表示一个不可变的、原始数据的类文件对象。通过Blob对象,可以创建一个指向文件数据的URL,用于下载文件。

function createDownloadLink(blob, filename) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement('a');
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  URL.revokeObjectURL(url);
}

3、实现批量下载

结合AJAX请求和Blob对象,可以实现批量下载图片的功能。下面是一个完整的实现示例:

async function downloadImages(urls, filenames) {
  for (let i = 0; i < urls.length; i++) {
    const url = urls[i];
    const filename = filenames[i];
    const blob = await fetchImage(url);
    createDownloadLink(blob, filename);
  }
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames);

二、使用文件下载库

除了手动实现批量下载图片的功能外,还可以使用现有的文件下载库,如FileSaver.js。这些库封装了文件下载的逻辑,使代码更加简洁和易于维护。

1、安装FileSaver.js

可以通过npm安装FileSaver.js:

npm install file-saver

2、使用FileSaver.js实现批量下载

import { saveAs } from 'file-saver';

async function fetchImage(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  return blob;
}

async function downloadImages(urls, filenames) {
  for (let i = 0; i < urls.length; i++) {
    const url = urls[i];
    const filename = filenames[i];
    const blob = await fetchImage(url);
    saveAs(blob, filename);
  }
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames);

三、处理并发请求

在批量下载大量图片时,处理并发请求可以提高下载效率。可以使用Promise.all来同时发送多个请求。

1、并发下载图片

async function fetchImage(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  return blob;
}

async function downloadImages(urls, filenames) {
  const promises = urls.map(fetchImage);
  const blobs = await Promise.all(promises);
  blobs.forEach((blob, index) => {
    const filename = filenames[index];
    createDownloadLink(blob, filename);
  });
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames);

2、限制并发数量

在某些情况下,限制并发请求的数量可以防止服务器过载。可以使用async/awaitsetTimeout来实现并发控制。

async function fetchImage(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  return blob;
}

async function downloadImages(urls, filenames, concurrency = 5) {
  let index = 0;

  async function downloadNext() {
    if (index >= urls.length) return;
    const url = urls[index];
    const filename = filenames[index];
    index++;
    const blob = await fetchImage(url);
    createDownloadLink(blob, filename);
    await downloadNext();
  }

  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(downloadNext());
  }
  await Promise.all(promises);
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames, 3);

四、错误处理和重试机制

在实际应用中,网络请求可能会失败,因此需要添加错误处理和重试机制,以确保所有图片都能成功下载。

1、添加错误处理

async function fetchImage(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
    }
    const blob = await response.blob();
    return blob;
  } catch (error) {
    console.error(error);
    return null;
  }
}

async function downloadImages(urls, filenames, concurrency = 5) {
  let index = 0;

  async function downloadNext() {
    if (index >= urls.length) return;
    const url = urls[index];
    const filename = filenames[index];
    index++;
    const blob = await fetchImage(url);
    if (blob) {
      createDownloadLink(blob, filename);
    }
    await downloadNext();
  }

  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(downloadNext());
  }
  await Promise.all(promises);
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames, 3);

2、实现重试机制

可以使用递归函数实现重试机制,如果请求失败,则重试指定次数。

async function fetchImage(url, retries = 3) {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
    }
    const blob = await response.blob();
    return blob;
  } catch (error) {
    if (retries > 0) {
      console.warn(`Retrying ${url}, attempts left: ${retries}`);
      return fetchImage(url, retries - 1);
    } else {
      console.error(`Failed to fetch ${url} after multiple attempts`);
      return null;
    }
  }
}

async function downloadImages(urls, filenames, concurrency = 5) {
  let index = 0;

  async function downloadNext() {
    if (index >= urls.length) return;
    const url = urls[index];
    const filename = filenames[index];
    index++;
    const blob = await fetchImage(url);
    if (blob) {
      createDownloadLink(blob, filename);
    }
    await downloadNext();
  }

  const promises = [];
  for (let i = 0; i < concurrency; i++) {
    promises.push(downloadNext());
  }
  await Promise.all(promises);
}

const imageUrls = [
  'https://example.com/image1.jpg',
  'https://example.com/image2.jpg',
  // 添加更多图片URL
];

const imageFilenames = [
  'image1.jpg',
  'image2.jpg',
  // 添加更多图片文件名
];

downloadImages(imageUrls, imageFilenames, 3);

总结

本文详细介绍了如何使用JS实现批量下载图片的方法,包括使用AJAX请求和Blob对象、文件下载库FileSaver.js、并发请求处理、错误处理和重试机制等。希望这些内容对你有所帮助,能够高效地实现批量下载图片的功能。

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号