nagose commited on
Commit
14517da
·
verified ·
1 Parent(s): 6c52ba2

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +41 -4
index.js CHANGED
@@ -12,6 +12,43 @@ const upload = multer({ storage: storage });
12
  app.use(express.json());
13
  app.use(express.urlencoded({ extended: true }));
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  app.post('/upload', upload.single('file'), async (req, res) => {
16
  try {
17
  const file = req.file;
@@ -20,9 +57,9 @@ app.post('/upload', upload.single('file'), async (req, res) => {
20
  const fileBuffer = file.buffer;
21
 
22
  // 输出接收到的文件信息
23
- console.log(`File Name: ${fileName}`);
24
- console.log(`File Type: ${fileType}`);
25
- console.log(`File Size: ${fileBuffer.length} bytes`);
26
 
27
  // 压缩文件成 zip 格式
28
  const zip = new JSZip();
@@ -52,7 +89,7 @@ app.post('/upload', upload.single('file'), async (req, res) => {
52
  Created At: ${new Date().toLocaleString()}
53
 
54
  `;
55
- ctx.fillText(text.trim(), 50, 100);
56
 
57
  // 将 canvas 转换为 JPEG 图片
58
  const rawImageData = canvas.toBuffer('image/jpeg', { quality: 0.75 });
 
12
  app.use(express.json());
13
  app.use(express.urlencoded({ extended: true }));
14
 
15
+ // 新增 GET / 路由来提供前端页面
16
+ app.get('/', (req, res) => {
17
+ res.send(`
18
+ <!DOCTYPE html>
19
+ <html lang="en">
20
+ <head>
21
+ <meta charset="UTF-8">
22
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
23
+ <title>File Upload</title>
24
+ </head>
25
+ <body>
26
+ <h1>Upload a File</h1>
27
+ <form id="uploadForm" action="/upload" method="post" enctype="multipart/form-data">
28
+ <input type="file" id="fileInput" name="file" required>
29
+ <button type="submit">Upload</button>
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
+ alert('File uploaded successfully!');
42
+ } else {
43
+ alert('Failed to upload file.');
44
+ }
45
+ });
46
+ </script>
47
+ </body>
48
+ </html>
49
+ `);
50
+ });
51
+
52
  app.post('/upload', upload.single('file'), async (req, res) => {
53
  try {
54
  const file = req.file;
 
57
  const fileBuffer = file.buffer;
58
 
59
  // 输出接收到的文件信息
60
+ //console.log(`File Name: ${fileName}`);
61
+ //console.log(`File Type: ${fileType}`);
62
+ //console.log(`File Size: ${fileBuffer.length} bytes`);
63
 
64
  // 压缩文件成 zip 格式
65
  const zip = new JSZip();
 
89
  Created At: ${new Date().toLocaleString()}
90
 
91
  `;
92
+ ctx.fillText(text.trim(), 10, 100);
93
 
94
  // 将 canvas 转换为 JPEG 图片
95
  const rawImageData = canvas.toBuffer('image/jpeg', { quality: 0.75 });