前端上传文件一直报错,后端显示读文件出问题,刚进函数在绑定参数的时候就出了问题返回了
明明都能打印出来但文件就是上传不上去,服务器会报错
400 {"msg": "multipart: NextPart: EOF",
"code": 40日,
"data": null}
400 {"msg": "multipart: NextPart: EOF","code": 400,
"data": null}
跟后端给的接口参数也是一样的
handleFileChange(event) {
this.upfile = event.target.files[0];
},
uploadCourseMaterial() {
if (!this.upfile) {
alert("请先选择文件");
return;
}
const formData = new FormData();
formData.append('file', this.upfile);
formData.append('courseId', parseInt(this.courseId));
formData.append('parentId', parseInt(this.parentId));
formData.append('private', this.isPrivate.toString());
formData.append('folder', this.isFolder.toString());
formData.append('filePic', parseInt(this.filePicId));
formData.append('folderName', this.folderName);
console.log(formData.get('file'))
console.log(formData.get('courseId'))
console.log(formData.get('parentId'))
console.log(formData.get('private'))
console.log(formData.get('folder'))
console.log(formData.get('filePic'))
console.log(formData.get('folderName'))
const boundary = '----' + Math.random().toString(36).slice(2, 17);
axios.post('/api/course/uploadFile', formData,{
headers: {
'Content-Type': `multipart/form-data; boundary=${boundary}`,
Authorization: this.$store.state.teacher.teacherData.token,
}
})
.then(response => response.json())
.then(data => {
// 处理响应数据
console.log(data);
alert("上传成功!");
this.clear();
})
.catch(error => {
console.error('Error:', error);
alert("上传失败,请重试!");
});
},