На странице имеется форма и кнопка для отправки файла (используется React):
const Upload = () => {
const [file, setFile] = useState();
const UploadContent = (event) => {
event.preventDefault();
if (event.target.files[0]) {
setFile(event.target.files[0]);
}
};
const OnSumbit = (event) => {
const formData = new FormData();
formData.append('customFile', file);
axios.post(
"http://localhost:3000/upload",
formData,
{
headers: {
"Content-type": "multipart/form-data"
},
}
)
.then(res => {
console.log(`Success` + res.data);
})
.catch(err => {
console.log(err);
})
}
return <div>
<h1 style={style}>Зона тестов</h1>
<input
accept="video/mp4"
id="contained-button-content"
multiple
type="file"
onChange={UploadContent}
/>
<Button variant="contained" color="primary" onClick={OnSumbit}>
Сохранить и закрыть
</Button>
</div>
}
На сервере используется files-uploader:
app.post('/upload', function(req, res) {
let sampleFile;
let uploadPath;
if (!req.file || Object.keys(req.file).length === 0) {
return res.status(400).send('No files were uploaded.');
}
sampleFile = req.file.customFile;
uploadPath = __dirname + '/uploads/' + sampleFile.name;
customFile.mv(uploadPath, function(err) {
if (err)
return res.status(500).send(err);
res.send('File uploaded!');
});
});
Не могу разобраться в чём дело, почему не загружается файл. Буду благодарен за любую помощь.