使用s3-zip生成文件后缀为.dms而非.zip的问题求助
S3-Zip生成文件被识别为.dms后缀的解决方法
问题详情
在Meteor项目中使用s3-zip工具压缩S3存储桶内的图片,生成的文件默认后缀为*.dms,但将其重命名为*.zip后可正常解压。搜索DMS相关结果多为数据库迁移内容,无法解决该问题。
相关代码
WebApp.connectHandlers.use('/download', (req, res, next) => { const opts = { Bucket: Meteor.settings.s3.bucket, region: Meteor.settings.s3.region, // Key: thing.assets[assetKey].path }; // 处理获取图片路径数组的逻辑... const poolImages = []; const directory = 'path/to/images'; // 准备生成压缩包 const archive = s3Zip .setFormat('zip') .archive({ s3: s3Client, region: opts.region, bucket: opts.Bucket, debug: true }, directory, poolImages) // .pipe(res) // .res(end); archive.on('error', (e) => { res.writeHead(500).end(e.message); }); // archive.on('end', () => { // console.log('archive created'); // }); archive.pipe(res).on('response', function(res) { res.writeHead(200, { 'Content-Type': 'application/zip' }); }); });
调试日志(流程正常)
I20220727-23:37:59.185(1)? append to zip d904450a3cd211c9cdccb4999d5c8f6e.jpg I20220727-23:37:59.198(1)? append to zip 94f7bc35c45ca6ee3976d00f5aa1aa28.jpg I20220727-23:37:59.205(1)? append to zip b3e6027e1ca57e4e800438106ff8ae30.jpg I20220727-23:37:59.206(1)? end -> finalize
问题原因及解决方法
问题出在响应头配置上:当前仅设置了Content-Type,但缺少Content-Disposition头,浏览器无法确定文件名和正确的文件类型,因此默认将文件识别为.dms格式。
修改响应头配置,添加Content-Disposition指定文件名(后缀为.zip):
archive.pipe(res).on('response', function(res) { res.writeHead(200, { 'Content-Type': 'application/zip', 'Content-Disposition': 'attachment; filename="images.zip"' }); });
这样浏览器会直接识别为zip文件下载,无需手动重命名。
内容的提问来源于stack exchange,提问作者Thomas Traum
相关产品推荐
相关产品推荐

