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:
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.cnfon Linux,my.inion Windows), add or update this line, then restart the MySQL service:max_allowed_packet=50M
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);
- Make sure you're using the correct MySQL column type to store the PDF. Don't use
BLOBorMEDIUMBLOB—they only support up to 64KB and 16MB respectively. UseLONGBLOBinstead, 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!'); }); });
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

