Update index.js
Browse files
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 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
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(),
|
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 |
-
//
|
117 |
-
const outputFileName = fileName.replace(/\.[^/.]+$/, "") + '-piczip.jpg';
|
118 |
-
|
119 |
-
// 返回新的隐藏 zip 文件的新图片文件
|
120 |
res.setHeader('Content-Type', 'image/jpeg');
|
121 |
-
res.setHeader('Content-Disposition', `attachment; filename
|
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);
|