如果我上传一个文件,代码工作完美。如果我想上传另一个文件,我得到这个Multer错误:
代码:"LIMIT_UNEXPECTED_FILE",:"myFile",storageErrors: []
如果我想成功上传另一个文件,我必须重新加载我的网页。
注意:我不想同时上传多个文件。我想上传一个文件,在服务器端计算它,一旦完成,我想上传另一个文件,等等。
客户机代码:
脚本:
let file_to_download;
let upload_button = document.getElementById('btnUpload');
let alert = document.getElementById('id_alert');
let form = document.getElementById('DownBtn');
form.style.visibility = "hidden";
let is_error = false;
const inpFile = document.getElementById('inpFile');
const xhr = new XMLHttpRequest();
const formData = new FormData();
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.status == 200) {
file_to_download = xhr.responseText;
} else {
let error = 'Error during upload. Try Again!';
is_error = true;
alert.innerHTML = "";
alert.innerHTML = error;
console.log(error);
}
}
};
upload_button.addEventListener('click', (e) => {
alert.innerHTML = "";
formData.append('myFile', inpFile.files[0]);
xhr.open('post', 'upload-ppt', false);
xhr.send(formData);
e.preventDefault();
if(is_error){
form.style.visibility = 'hidden';
is_error = false;
}else{
form.style.visibility = 'visible';
}
document.getElementById('inpFile').value = '';
});
Html:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Polly</title>
<script src="convert_settings.js" charset="utf-8" defer></script>
</head>
<body>
<form>
<div id="UpBtn">
<input type="file" id="inpFile">
<button id="btnUpload">Upload file</button>
</div>
</form>
<form id="DownBtn">
<div>
<button>Download</button>
</div>
</form>
<div id="id_alert"></div>
</body>
</html>
服务器端:
const express = require('express');
const multer = require('multer');
const path = require('path');
const {startApp} = require('./GUI');
let file_to_download;
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'SharedFolder/pptx/');
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
cb(null, uniqueSuffix + path.extname(file.originalname)); //Appending extension
}
})
//TO DO
//Check if file extension is pptx or ppt
function uploadFile(req, res, next) {
const upload = multer({storage: storage}).single('myFile');
//single('myFile');
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
console.log('Multer error occured:');
console.log(err);
} else if (err) {
// An unknown error occurred when uploading.
console.log('Unknown error occured');
}
// Everything went fine.
next()
})
}
const app = express();
const bodyParser = require('body-parser');
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.post('/upload-ppt', uploadFile, startPolly, (req,res) => {
//console.log(req.file.filename);
if(req.file == null){
res.sendStatus(400);
}else{
res.send(req.file.filename);
}
}
);
app.post('/download',(req,res) =>{
file_to_download = './downloads/' + req.body.dwnFile;
res.download(file_to_download);
});
async function startPolly(req, res, next){
if(req.file != null){
if(req.file.filename != null){
await startApp(req.file.filename);
}
}
next();
};
//start app
const port = 3000;
app.listen(port, () =>
console.log(`App is listening on port ${port}.`)
);
在第二篇文章中,您添加了相同的文件字段并发送了之前的表单
您需要在每次文件上传时重置表单,因此将其与文件一起移动到按钮单击处理程序中:
const formData = new FormData();
const inpFile = document.getElementById('inpFile');
formData.append('myFile', inpFile.files[0]);