我有一个express
上传文件的路由,这些文件通过formData
.
假设文件是一个.rar
或.zip
文件,我的目标是提取所有文件名,即在这个压缩文件夹或其子文件夹中。
这是我的express
路线目前的样子:
module.exports = async (req, res) => {
try {
const busboy = new Busboy({ headers: req.headers })
busboy.on('finish', async () => {
const fileData = req.files.file
console.log(fileData)
// upload file
// send back response
})
req.pipe(busboy)
} catch (err) { return response.error(req, res, err, 'uploadProductFile_unexpected') }
}
这是console.log(fileData)
外观:
{
data:
<Buffer 52 61 72 21 1a 07 01 00 56 0c 22 93 0c 01 05 08 00 07 01 01 8d d6 8d 80 00 85 76 33 e4 49 02 03 0b fc d4 0d 04 b1 8c 1e 20 bc 86 da 2e 80 13
00 2b 66 ... >,
name: 'filename.rar',
encoding: '7bit',
mimetype: 'application/octet-stream',
truncated: false,
size: 224136
}
里面filename.rar
有一些文件,比如texture.png
和info.txt
。我的目标是获取这些名字。
您应该能够通过使用 JSZip 的 .files 函数来完成此操作。
var fs = require("fs");
var JSZip = require("jszip");
// read a zip file
fs.readFile("project.zip", function(err, data) {
if (err) throw err;
JSZip.loadAsync(data).then(function (zip) {
files = Object.keys(zip.files);
console.log(files);
});
});
是此类解决方案的一个示例,由本教程提供。您应该能够从req
请求中对您的文件使用类似的功能。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句