我需要您的帮助我正在尝试使用express fileupload在代码中上载文件,但在nodemon中,req.files始终为空,但我发现我的文件属性传递到req.body
这是我的前端React代码:
import Message from './Message';
import Progress from './Progress';
import axios from 'axios';
const FileUpload = () => {
const [file, setFile] = useState('');
const [filename, setFilename] = useState('Choose File');
const [uploadedFile, setUploadedFile] = useState({});
const [message, setMessage] = useState('');
const [uploadPercentage, setUploadPercentage] = useState(0);
const onChange = e => {
console.log(e.target.files[0].name)
setFile([e.target.files[0]]);
setFilename(e.target.files[0].name);
console.log(file)
};
const onSubmit = async e => {
const formData = new FormData(); console.log(file)
formData.append('file', file);
if(!formData){
console.log("empty");}
for (var key of formData.entries()) {
console.log(key[0][0] + ', ' + key[1][0])
}
try {
const res = await axios.post('http://localhost:5001/up', formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
onUploadProgress: progressEvent => {
setUploadPercentage(
parseInt(
Math.round((progressEvent.loaded * 100) / progressEvent.total)
)
);
}
});
console.log("file read");
// Clear percentage
setTimeout(() => setUploadPercentage(0), 10000);
console.log(res)
const { fileName, filePath } = res.data;
setUploadedFile({ fileName, filePath });
setMessage('File Uploaded');
} catch (err) {
console.log(err)
setUploadPercentage(0)
}
};
return (
<Fragment>
{message ? <Message msg={message} /> : null}
<form onSubmit={onSubmit} enctype='multipart/form-data'>
<div className='custom-file mb-4'>
<input
type='file'
enctype='multipart/form-data'
className='custom-file-input'
id='customFile'
onChange={onChange}
/>
<label className='custom-file-label' htmlFor='customFile'>
{filename}
</label>
</div>
here: {filename}
<Progress percentage={uploadPercentage} />
<input
type='submit'
value='Upload'
className='btn btn-primary btn-block mt-4'
/>
</form>
{uploadedFile ? (
<div className='row mt-5'>
<div className='col-md-6 m-auto'>
<h3 className='text-center'>{uploadedFile.fileName} kkkk</h3>
<img style={{ width: '100%' }} src={uploadedFile.filePath} alt='' />
</div>
</div>
) : null}
</Fragment>
);
};
export default FileUpload;
在这里,我试图知道我发送的fromdata是否为空,但这不是我认为存在的问题,还有后端:
import cors from "cors"
import nez_topographie from "./api/controllers/nez_topographie.route.js"
import fileUpload from "express-fileupload"
import bodyParser from "body-parser"
import morgan from "morgan"
const app = express()
app.use(cors())
app.use(express.json())
app.use(fileUpload());
app.post('/up', async (req, res) => {
try {
console.log(req)
console.log(req.body.file)
console.log(req.files)
if(!req.files) {
console.log("null")
res.send({
status: false,
message: 'No file uploaded'
});
} else {
//Use the name of the input field (i.e. "avatar") to retrieve the uploaded file
let avatar = req.files.file;
//Use the mv() method to place the file in upload directory (i.e. "uploads")
avatar.mv('./' + avatar.name);
//send response
res.send({
status: true,
message: 'File is uploaded',
data: {
name: avatar.name,
mimetype: avatar.mimetype,
size: avatar.size
}
});
}
} catch (err) {
console.log(err)
res.status(500).send(err);
}
});
export default app
在我的控制台中,我提供资金:[console.log(req)][1]
我确信我的formdata不是空的
另外,我发现这个解决方案:[解决方案不起作用][2]
但是“:任何”不工作:[发现错误][3]
我认为快速文件上传不起作用,但我也尝试在表单和输入中添加enctype='multipart/form data',但req.files始终为空。希望您能帮助我谢谢您的帮助:)[1]:https://i.stack.imgur.com/vboxl.png [2]: https://i.stack.imgur.com/e3ldm.png [3]: https://i.stack.imgur.com/sey8q.png
暂无答案!
目前还没有任何答案,快来回答吧!