单独上传xlsx没有问题,但是多加一条字符串报错:org.springframework.web.multipart.MultipartException: The current request is not a multipart request
以下是我的代码:
jsp:
<div class="btn-file col-md-6">
<button id="btn_upload" type="button" class="btn btn-success" style="width: 100px;margin-right: 20px">
<i class="fa fa-cloud-upload" aria-hidden="true"></i> 文件上传
</button>
</div>
js:
$("#btn_upload").on("click", function () {
var date = $("#date").val();
var s = document.getElementById("fileUpload");
if (s !== undefined) {
$("#fileUpload").remove();
}
var inputObj = document.createElement('input');
inputObj.setAttribute('id', 'fileUpload');
inputObj.setAttribute('type', 'file');
inputObj.setAttribute('name', 'file');
inputObj.setAttribute('accept', '.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel');
inputObj.setAttribute("style", 'visibility:hidden');
document.body.appendChild(inputObj);
inputObj.click();
$("#fileUpload").on('change', function () {
$("#inputText1").val(this.files[0].name);
if ($("#fileUpload").val() !== "") {
var formData = new FormData();
formData.append("file",this.files[0]);
$.ajax({
type: "post",
url: appName + '/report/upload.do',
data: {
data:data,
file:formData
},
dataType: "text",
processData: false,
contentType: false,
success: function (result) {
if (result == "success"){
layer.msg("提交成功!", {icon: 6, time: 3000});
} else {
layer.msg("提交失败!", {icon: 2, time: 3000});
}
},
error: function () {
layer.msg("请求失败!", {icon: 2, time: 3000});
}
});
layer.msg('文件上传!', {icon: 1, time: 3000});
} else {
layer.msg("请先选择文件!");
}
});
});
controller:
@RequestMapping(value="/upload.do")
@ResponseBody
public String upload(String date, MultipartFile file) throws IOException {
String path = "F:\\upload";
String fileName = file.getOriginalFilename();
File dir = new File(path,fileName);
if(!dir.exists()){
dir.mkdirs();
}
//MultipartFile自带的解析方法
file.transferTo(dir);
System.out.println(date);
System.out.println(fileName);
return "fail";
}