nagose commited on
Commit
1b9102d
·
verified ·
1 Parent(s): 7809dc3

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +30 -53
index.js CHANGED
@@ -2,7 +2,6 @@ const express = require('express');
2
  const multer = require('multer');
3
  const JSZip = require('jszip');
4
  const { createCanvas } = require('canvas');
5
- const fs = require('fs');
6
  const app = express();
7
 
8
  // 设置 multer 用于处理文件上传
@@ -30,35 +29,32 @@ app.get('/', (req, res) => {
30
  </form>
31
  <script>
32
  document.getElementById('uploadForm').addEventListener('submit', async (event) => {
33
- event.preventDefault();
34
- const formData = new FormData(event.target);
35
- const response = await fetch(event.target.action, {
36
- method: 'POST',
37
- body: formData
38
- });
39
-
40
- if (response.ok) {
41
- // 假设服务器返回的是一个可下载的文件
42
- response.blob().then(blob => {
43
- // 创建一个链接元素
44
- const url = window.URL.createObjectURL(blob);
45
- const a = document.createElement('a');
46
- a.style.display = 'none';
47
- a.href = url;
48
- // 从Content-Disposition头中提取文件名
49
- const contentDisposition = response.headers.get('Content-Disposition');
50
- const fileName = contentDisposition.match(/filename="(.+)"/)[1];
51
- a.download = fileName;
52
- document.body.appendChild(a);
53
- a.click();
54
- window.URL.revokeObjectURL(url);
55
- alert('File downloaded successfully!');
56
- });
57
- } else {
58
- alert('Failed to upload file.');
59
- }
60
- });
61
-
62
  </script>
63
  </body>
64
  </html>
@@ -72,11 +68,6 @@ app.post('/upload', upload.single('file'), async (req, res) => {
72
  const fileType = file.mimetype;
73
  const fileBuffer = file.buffer;
74
 
75
- // 输出接收到的文件信息
76
- console.log(`File Name: ${fileName}`);
77
- //console.log(`File Type: ${fileType}`);
78
- //console.log(`File Size: ${fileBuffer.length} bytes`);
79
-
80
  // 压缩文件成 zip 格式
81
  const zip = new JSZip();
82
  zip.file(fileName, fileBuffer);
@@ -87,38 +78,24 @@ app.post('/upload', upload.single('file'), async (req, res) => {
87
  const height = 300;
88
  const canvas = createCanvas(width, height);
89
  const ctx = canvas.getContext('2d');
90
-
91
- // 设置背景为白色
92
  ctx.fillStyle = '#FFFFFF';
93
  ctx.fillRect(0, 0, width, height);
94
-
95
- // 设置文本颜色为黑色,并绘制文本
96
  ctx.fillStyle = '#000000';
97
  ctx.font = '20px Arial';
98
  const text = `
99
  File Name: ${fileName}
100
-
101
  File Type: ${fileType}
102
-
103
  File Size: ${fileBuffer.length} bytes
104
-
105
  Created At: ${new Date().toLocaleString()}
106
-
107
  `;
108
- ctx.fillText(text.trim(), 5, 100);
109
-
110
- // 将 canvas 转换为 JPEG 图片
111
  const rawImageData = canvas.toBuffer('image/jpeg', { quality: 0.75 });
112
-
113
- // 将包含信息的图片和生成的 zip 打包
114
  const finalBuffer = Buffer.concat([rawImageData, zipBuffer]);
 
115
 
116
- // 生成新的文件名,添加后缀 -piczip
117
- const outputFileName = fileName.replace(/\.[^/.]+$/, "") + '-piczip.jpg';
118
-
119
- // 返回新的隐藏 zip 文件的新图片文件
120
  res.setHeader('Content-Type', 'image/jpeg');
121
- res.setHeader('Content-Disposition', `attachment; filename="${outputFileName}"`);
122
  res.send(finalBuffer);
123
  } catch (error) {
124
  console.error(error);
 
2
  const multer = require('multer');
3
  const JSZip = require('jszip');
4
  const { createCanvas } = require('canvas');
 
5
  const app = express();
6
 
7
  // 设置 multer 用于处理文件上传
 
29
  </form>
30
  <script>
31
  document.getElementById('uploadForm').addEventListener('submit', async (event) => {
32
+ event.preventDefault();
33
+ const formData = new FormData(event.target);
34
+ const response = await fetch(event.target.action, {
35
+ method: 'POST',
36
+ body: formData
37
+ });
38
+ if (response.ok) {
39
+ response.blob().then(blob => {
40
+ const url = window.URL.createObjectURL(blob);
41
+ const a = document.createElement('a');
42
+ a.style.display = 'none';
43
+ a.href = url;
44
+ // 从Content-Disposition头中提取文件名
45
+ const contentDisposition = response.headers.get('Content-Disposition');
46
+ const filenameMatch = contentDisposition.match(/filename\*=UTF-8''(.+)/);
47
+ const fileName = filenameMatch ? decodeURIComponent(filenameMatch[1]) : 'downloaded.zip';
48
+ a.download = fileName;
49
+ document.body.appendChild(a);
50
+ a.click();
51
+ window.URL.revokeObjectURL(url);
52
+ alert('File downloaded successfully!');
53
+ });
54
+ } else {
55
+ alert('Failed to upload file.');
56
+ }
57
+ });
 
 
 
58
  </script>
59
  </body>
60
  </html>
 
68
  const fileType = file.mimetype;
69
  const fileBuffer = file.buffer;
70
 
 
 
 
 
 
71
  // 压缩文件成 zip 格式
72
  const zip = new JSZip();
73
  zip.file(fileName, fileBuffer);
 
78
  const height = 300;
79
  const canvas = createCanvas(width, height);
80
  const ctx = canvas.getContext('2d');
 
 
81
  ctx.fillStyle = '#FFFFFF';
82
  ctx.fillRect(0, 0, width, height);
 
 
83
  ctx.fillStyle = '#000000';
84
  ctx.font = '20px Arial';
85
  const text = `
86
  File Name: ${fileName}
 
87
  File Type: ${fileType}
 
88
  File Size: ${fileBuffer.length} bytes
 
89
  Created At: ${new Date().toLocaleString()}
 
90
  `;
91
+ ctx.fillText(text.trim(), 10, 100);
 
 
92
  const rawImageData = canvas.toBuffer('image/jpeg', { quality: 0.75 });
 
 
93
  const finalBuffer = Buffer.concat([rawImageData, zipBuffer]);
94
+ const outputFileName = encodeURIComponent(fileName.replace(/\.[^/.]+$/, "") + '-piczip.jpg');
95
 
96
+ // 使用RFC 5987编码标准设置Content-Disposition头
 
 
 
97
  res.setHeader('Content-Type', 'image/jpeg');
98
+ res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${outputFileName}`);
99
  res.send(finalBuffer);
100
  } catch (error) {
101
  console.error(error);