使用VUE上传excel到JAVA接口,为什么接收不到file
使用VUE上传excel到JAVA接口,为什么接收不到file
JAVA代码
@ApiOperation("导入EXCEL")
@RequestMapping(value="/v1/bulldozer-info/excel", method={ RequestMethod.POST })
public ResponseResult upload(
HttpServletRequest request,
MultipartFile file) throws IOException {
String code = "500";
String message = "FAIL";
String detail = "";
ResponseResult responseResult = new ResponseResult();
log.info("提交");
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=enu.nextElement();
log.info(paraName+": "+request.getParameter(paraName));
}
System.out.println("file == " + file);
List<ExcelInfo> excelList = OfficeUtil.importExcel(file);
if(excelList != null) {
code = "200";
message = "SUCCESS";
detail = "insertTaskVersion";
int excelListNum = bulldozerInfoService.insertExcelInfo(excelList);
responseResult.setData(excelListNum);
}
responseResult.setCode(code);
responseResult.setMessage(message);
responseResult.setDetail(detail);
return responseResult;
}
VUE代码
let form = new FormData();
form.append('file', this.files);
Axios({
method:"post",
url: "/bulldozerTest/v1/bulldozer-info/excel",
headers:{
'Content-type': 'multipart/form-data'
},
data:form
}).then(
res=>{
console.log(res);
},err =>{
console.log("result == ", err)
}
);
新的错误
@ApiOperation("导入EXCEL")
@RequestMapping(value="/v1/bulldozer-info/excel", method={ RequestMethod.POST })
public ResponseResult upload(
HttpServletRequest request,
@RequestParam("file") MultipartFile file) throws IOException {
String code = "500";
String message = "FAIL";
String detail = "";
ResponseResult responseResult = new ResponseResult();
log.info("提交");
Enumeration<String> enu=request.getParameterNames();
while(enu.hasMoreElements()){
String paraName=enu.nextElement();
log.info(paraName+": "+request.getParameter(paraName));
}
System.out.println("file == " + file);
List<ExcelInfo> excelList = OfficeUtil.importExcel(file);
if(excelList != null) {
code = "200";
message = "SUCCESS";
detail = "insertTaskVersion";
int excelListNum = bulldozerInfoService.insertExcelInfo(excelList);
responseResult.setData(excelListNum);
}
responseResult.setCode(code);
responseResult.setMessage(message);
responseResult.setDetail(detail);
return responseResult;
}
前端代码问题
fileFormData.append('filesUpload', _this.files);
改为
fileFormData.append('filesUpload', _this.files.raw);
上传excel函数
submitExcel() {
const _this = this
console.log('上传文件列表 == '+ _this.files.name)
if(_this.fileName == ""){
_this.$message.warning('请选择要上传的文件!')
return false
}
let fileFormData = new FormData();
// fileFormData.append("code", "t_pathology_info_excel");
// fileFormData.append("description", "excel上传测试");
//filename是键,file是值,就是要传的文件,test是要传的文件名
// fileFormData.append('files', this.files, this.fileName);
fileFormData.append('filesUpload', _this.files.raw);
// fileFormData.append('filesUpload', _this.files);
// 执行上传excel
let id = '';
// this.http.postJson('/sys/file/upload', fileFormData, requestConfig).then(resp => {
// if (resp.data.status != 200) {
// this.$message.error("excel上传失败,请重新上传");
// } else {
// id = resp.data.result[0].attachmentId;
// }
// }).catch((e) => {
// // console.log(e);
// this.$message.error("excel上传失败,请重新上传");
// }).finally(() => {
// if(id) {
// // 触发生成订单实体数据
// this.generateBill(id);
// }
// });
// let form = new FormData();
// form.append('filesUpload', this.files);
// Axios({
// method:"post",
// url: "/bulldozerTest/v1/bulldozer-info/excel",
// headers:{
// 'Content-type': 'multipart/form-data'
// },
// data:form
// }).then(
// res=>{
// console.log(res);
// },err =>{
// console.log("result == ", err)
// }
// );
let dataReq = {
url: '/v1/bulldozer-info/excel',
headers: {
'Content-Type': 'multipart/form-data'
},
data: fileFormData
}
addLeaveRecordApi(dataReq).then( res=>{
console.log('getLoginWeb res == ', res)
_this.handleAddResData(res)
}).catch( err =>{
console.log('getLogin err == ', err)
_this.$message({
// message: err.status + ' - ' + err.statusText,
message: err.status + ' - ',
type:'error',
duration: '3000'
})
})
},
// 构建实体
generateBill(attachmentId) {
this.http.postJson('/admin/myTest/leadingIn/' + attachmentId + '/type_one').then(resp => {
if (resp.data.status !== 200) {
// this.$message.error("excel上传失败,请重新上传");
} else {
this.$message.success("excel上传成功!");
this.query();
}
}).catch((e) => {
// console.log(e);
// this.$message.error("excel上传失败,请重新上传");
}).finally(() => {
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MultipartFile file
改成
@RequestParam("file") MultipartFile file