Browse Source

fix: 跳转计量台账附件zip存储名称,防止乱码

lanjianrong 4 years ago
parent
commit
71eedc5b03
1 changed files with 35 additions and 29 deletions
  1. 35 29
      app/controller/stage_controller.js

+ 35 - 29
app/controller/stage_controller.js

@@ -1524,36 +1524,42 @@ module.exports = app => {
          * @param {Object} ctx - 全局上下文
          */
         async downloadZip(ctx) {
-            const fileIds = JSON.parse(ctx.request.query.fileIds);
-            const zipFilename = `${ctx.tender.data.name}-计量台账-${ctx.params.order}-附件.zip`;
-            const zipPath = `app/public/upload/${ctx.tender.id}/stage/${zipFilename}`;
-            const size = await ctx.service.stageAtt.compressedFile(fileIds, zipPath);
-
-            // 解决中文无法下载问题
-            const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
-            let disposition = '';
-            if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
-                disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
-            } else if (userAgent.indexOf('firefox') >= 0) {
-                disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
-            } else {
-                /* safari等其他非主流浏览器只能自求多福了 */
-                disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
-            }
-            ctx.response.set({
-                'Content-Type': 'application/octet-stream',
-                'Content-Disposition': disposition,
-                'Content-Length': size,
-            });
-            // const readStream = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
-            // ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
-            ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
-            // readStream.on('close', () => {
-
-            // });
-            if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
-                fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
+            try {
+                const fileIds = JSON.parse(ctx.request.query.fileIds);
+                const zipFilename = `${ctx.tender.data.name}-计量台账-${ctx.params.order}-附件.zip`;
+                const time = Date.now();
+                const zipPath = `app/public/upload/${ctx.tender.id}/stage/fu_jian_zip${time}.zip`;
+                const size = await ctx.service.stageAtt.compressedFile(fileIds, zipPath);
+
+                // 解决中文无法下载问题
+                const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
+                let disposition = '';
+                if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
+                    disposition = 'attachment; filename=' + encodeURIComponent(zipFilename);
+                } else if (userAgent.indexOf('firefox') >= 0) {
+                    disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(zipFilename) + '"';
+                } else {
+                    /* safari等其他非主流浏览器只能自求多福了 */
+                    disposition = 'attachment; filename=' + new Buffer(zipFilename).toString('binary');
+                }
+                ctx.response.set({
+                    'Content-Type': 'application/octet-stream',
+                    'Content-Disposition': disposition,
+                    'Content-Length': size,
+                });
+                // const readStream = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
+                // ctx.body = fs.createReadStream(path.resolve(this.app.baseDir, zipPath));
+                ctx.body = fs.readFileSync(path.resolve(this.app.baseDir, zipPath));
+                // readStream.on('close', () => {
+
+                // });
+                if (fs.existsSync(path.resolve(this.app.baseDir, zipPath))) {
+                    fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
+                }
+            } catch (error) {
+                this.log(error);
             }
+
         }
         /**
          * 合同支付上传附件