简体   繁体   中英

How to download multiple files bundled into one zip files in Node js?

I am working on node project, In my project I have two images in the images folder. Now my goal is I have to zip those two images and download. For this I am using this npm zip-downloader .

But I am getting these kind of errors

Error: Cannot find module 'babel-runtime/core-js/object/assign'

This is my code, server.js

const express = require('express');
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const multer = require('multer');
const upload = multer({dist:'./uploads'});
const jimp = require('jimp');
const zip = require('file-zip');
const downloader = require('zip-downloader')





app.post('/api/images',upload.single('profilepic'), (req, res) =>{
    console.log(req.file)
    res.json({'message':'file upload successfully'})
});


jimp.read('images/one.jpeg')
  .then(one => {
    return morng
      .resize(100, 100) // resize
      .quality(60) // set JPEG quality
      .greyscale() // set greyscale
      .write('images/two.jpg'); // save
  })
  .catch(err => {
    console.error(err);
  });


zip.zipFile(['images/one.jpeg','images/two.jpg'],'out.zip',function(err){
    if(err){
        console.log('zip error',err)
    }else{
        console.log('zip success');
    }
})

const assets = [
  {
      'src': 'images/one.jpeg'
  },
  {
    'src': 'images/two.jpg'
  }
];

const options = {
  downloadFolderName: 'images',
  statusCallback: function(downloadedTillNow){
      console.log('Download status:' + ((downloadedTillNow * 100)/assets.length));
  },
  onComplete : function(downloadedSummary){
      console.log('Assets downloaded:' + downloadedSummary.numberOfDownloadedAssets);
      console.log('Assets failed:' + downloadedSummary.numberOfFailedAssets);
      console.log('Large Assets downloaded(over maxZIPSize):' + downloadedSummary.numberOfLargeUnZippedAssets);
      console.log('Number of zip files downloaded:' + downloadedSummary.numberOfDownloadedZIPFiles);
      console.log('Array of failed assets:');
      console.log(downloadedSummary.failedAssetList);
  },
};

downloader(assets, options);

This is package.json file

{
  "name": "file",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "file-zip": "^1.0.1",
    "files-download-zip": "^3.1.1",
    "jimp": "^0.9.3",
    "jszip": "^3.2.2",
    "multer": "^1.4.2",
    "zip-downloader": "^1.0.2"
  }
}

Need a solution to overcome this error.

Since you're defining an object const options = {} , all of the properties which are defined inside the bracket {} should follow by :

The error in this case:

SyntaxError: Invalid shorthand property initializer

means you're trying to create a property with invalid syntax.

You can fix it by:

const options = {
  downloadFolderName: 'images',
  statusCallback: function(downloadedTillNow){
      console.log('Download status:' + ((downloadedTillNow * 100)/assets.length));
  },
  onComplete: function(downloadedSummary){
      console.log('Assets downloaded:' + downloadedSummary.numberOfDownloadedAssets);
      console.log('Assets failed:' + downloadedSummary.numberOfFailedAssets);
      console.log('Large Assets downloaded(over maxZIPSize):' + downloadedSummary.numberOfLargeUnZippedAssets);
      console.log('Number of zip files downloaded:' + downloadedSummary.numberOfDownloadedZIPFiles);
      console.log('Array of failed assets:');
      console.log(downloadedSummary.failedAssetList);
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM