[axios] How to resume downloading binary excel files in axios

Use Axios to download binary Excel files and interrupt and resume downloads. Here's a basic way to do it:

1. Before starting the download, make sure that Axios has been used in your project (can be installed by using npm or yarn).

2. Create a function for downloading and use axios.get() to initiate a file download request. As follows:

const axios = require('axios');
const fs = require('fs');

// 下载函数
async function downloadExcelFile(url, outputPath, headers = {
     
     }) {
    
    
  const writer = fs.createWriteStream(outputPath, {
    
     flags: 'a' });
  
  const response = await axios.get(url, {
    
    
    responseType: 'stream',
    headers,
  });

  response.data.pipe(writer);

  // 返回一个 Promise,以便在外部处理下载过程
  return new Promise((resolve, reject) => {
    
    
    writer.on('finish', resolve);
    writer.on('error', reject);
  });
}

// 调用下载函数
downloadExcelFile('http://example.com/excel-file.xls', 'output.xls')
  .then(() => {
    
    
    console.log('文件下载完成');
  })
  .catch((error) => {
    
    
    console.error('文件下载失败', error);
  });

3. Add support for interruption and resuming downloads to the existing download function. You can use the range request header to control the requested block position. Modify the download function as follows:

const axios = require('axios');
const fs = require('fs');

// 下载函数
async function downloadExcelFile(url, outputPath, headers = {
     
     }) {
    
    
  let startByte = 0;

  if (fs.existsSync(outputPath)) {
    
    
    // 如果已经存在输出文件,则获取已下载的文件大小
    const stats = fs.statSync(outputPath);
    startByte = stats.size;

    // 通过设置 Range 请求头来指定从哪个位置继续下载
    headers['Range'] = `bytes=${
      
      startByte}-`;
  }

  const writer = fs.createWriteStream(outputPath, {
    
     flags: 'a' });

  const response = await axios.get(url, {
    
    
    responseType: 'stream',
    headers,
  });

  response.data.pipe(writer);

  // 返回一个 Promise,以便在外部处理下载过程
  return new Promise((resolve, reject) => {
    
    
    writer.on('finish', resolve);
    writer.on('error', reject);
  });
}

// 调用下载函数
downloadExcelFile('http://example.com/excel-file.xls', 'output.xls')
  .then(() => {
    
    
    console.log('文件下载完成');
  })
  .catch((error) => {
    
    
    console.error('文件下载失败', error);
  });

With the above modification, the download function will check whether the output file already exists. If it exists, use Rangethe request header to specify that the download will continue starting from the downloaded file size, realizing the function of interrupting and resuming the download.

Please note that the server must support and correctly handle Rangerequest headers while ensuring correct transfer and resumption of file content. Make sure the response header information is set correctly on the server side. The front-end breakpoint resume download is actually based on server-side support and protocols.

Guess you like

Origin blog.csdn.net/hzxOnlineOk/article/details/133087091