[英]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
.为此,我正在使用这个 npm
zip-downloader
。
But I am getting these kind of errors但我收到了这些错误
Error: Cannot find module 'babel-runtime/core-js/object/assign'错误:找不到模块“babel-runtime/core-js/object/assign”
This is my code, server.js
这是我的代码
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这是
package.json
文件
{
"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 :
由于您正在定义一个对象
const options = {}
,因此在括号{}
内定义的所有属性都应遵循:
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);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.