You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js Express无法上传大PDF至MySQL,需扩容至50MB

Hey there! Let's break down why you're hitting that error even after setting the 50MB limits. Here are the most common culprits and fixes to get your large PDF uploads working smoothly:

1. Fix MySQL's max_allowed_packet setting

This is the #1 reason large files fail to save to MySQL even after uploading successfully to your server. By default, MySQL's max_allowed_packet is usually set to a small value (like 4MB or 16MB), which can't handle a 50MB file.

  • Temporary fix (resets on MySQL restart):
    Run this command in your MySQL terminal:
    SET GLOBAL max_allowed_packet = 52428800; -- 50MB in bytes
    
  • Permanent fix:
    Locate your MySQL config file (my.cnf on Linux, my.ini on Windows), add or update this line, then restart the MySQL service:
    max_allowed_packet=50M
    
2. Increase Express server timeouts

Uploading a 50MB file takes longer than small files, and the default server timeout might cut the connection mid-process.

Add this middleware to your Express app to extend timeouts (adjust the value based on your needs—here we use 5 minutes):

app.use((req, res, next) => {
  req.setTimeout(300000); // 5 minutes in milliseconds
  res.setTimeout(300000);
  next();
});

If you're creating the server directly with Node.js's http module, set the timeout there too:

const http = require('http');
const server = http.createServer(app);
server.timeout = 300000; // 5 minutes
server.listen(3000);
3. Validate your file handling logic & MySQL column type
  • Make sure you're using the correct MySQL column type to store the PDF. Don't use BLOB or MEDIUMBLOB—they only support up to 64KB and 16MB respectively. Use LONGBLOB instead, which can handle files up to 4GB.
  • Double-check your route handler for any hardcoded size limits you might have missed. Here's a safe example of handling the upload:
app.post('/upload-pdf', (req, res) => {
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.');
  }

  const pdfFile = req.files.pdfFile;
  
  // Optional: Double-check file size as a safety net
  if (pdfFile.size > 50 * 1024 * 1024) {
    return res.status(413).send('File too large. Max allowed is 50MB.');
  }

  // Save to MySQL using LONGBLOB column
  const query = 'INSERT INTO pdf_storage (file_name, file_content) VALUES (?, ?)';
  db.query(query, [pdfFile.name, pdfFile.data], (err, result) => {
    if (err) {
      console.error('Database save error:', err);
      return res.status(500).send('Failed to save file to database.');
    }
    res.send('PDF uploaded and saved successfully!');
  });
});
4. Check reverse proxy settings (if using Nginx/Apache)

If your Express server sits behind a reverse proxy like Nginx or Apache, these tools have their own upload limits and timeouts that can block large files:

For Nginx:

Add these lines to your site's config file or nginx.conf, then restart Nginx:

client_max_body_size 50M;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;

For Apache:

Add these lines to httpd.conf or your .htaccess file:

LimitRequestBody 52428800 # 50MB in bytes
Timeout 300 # 5 minutes

Start with the MySQL max_allowed_packet fix first—it's the most common issue when files make it to the server but fail to save to the database. Work through the other checks one by one, and you should have your 50MB PDF uploads working smoothly!

内容的提问来源于stack exchange,提问作者Sadman Sakib Hasan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:51:02