我正在尝试在React中构建一个简单的文件上传表单,该表单发布到Node.js API。我正在使用名为Multer的NPM软件包来处理上传。我在以前的项目之前使用过此设置并且工作正常但由于某种原因我尝试上传时收到500错误。
这是我的UploadForm组件:
import React, { Component } from 'react';
import axios from 'axios';
class GuestListUploadForm extends Component {
render() {
return (
<section className="py-4">
<div className="container">
<h2>Upload A Guestlist</h2>
<form action="/upload-guestlist" method="post" className="border p-3" encType="multipart/form-data">
<label htmlFor="file" className="d-block">Choose an Excel spreadsheet file to upload your guestlist</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" name="Submit"/>
</form>
</div>
</section>
)
}
}
export default GuestListUploadForm;
&#13;
和我的node.js guestController.js:
const { promisify } = require('util');
const multer = require('multer');
const uuidv4 = require('uuidv4');
const path = require('path');
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './uploads/')
},
filename: (req, file, cb) => {
const newFilename = `${uuidv4()}${path.extname(file.originalname)}`;
cb(null, newFilename)
},
});
exports.uploadGuestListFile = (req, res, next) => {
console.log('multer');
multer({ storage }).single('file');
next();
};
&#13;
任何帮助都会很棒。