1.利用from表单及ajax处理
<form id="uploadForm" enctype="multipart/form-data" method="post">
<input id='uploadFile' name="uploadFile" class="easyui-filebox" buttonAlign="left" data-options="prompt:'选择文件',buttonText:'选择文件'" style="width:250px"/>
</form>
var form = new FormData(document.getElementById("uploadForm"));
$.ajax({
url: "/uploadFiles",
type: "post",
data:form,
cache: false,//上传文件无需缓存
processData: false,// 告诉jquery要传输data对象
contentType: false,// 告诉jquery不需要增加请求头对于contentType的设置
beforeSend: function(){
$("<p class=\"datagrid-mask\"></p>").css({ display: "block", width: "100%", height: $(window).height() }).appendTo("body");
$("<p class=\"datagrid-mask-msg\"></p>").html("正在运行,请稍候...").appendTo("body").css({ display: "block", left: ($(document.body).outerWidth(true) - 80) / 2, top: ($(window).height() - 45) / 2 });
},
complete: function(){
$(".datagrid-mask").remove();
$(".datagrid-mask-msg").remove();
},
success: function (data) {
alert("上传成功!");
},
error: function () {
alert("上传失败!");
}
});
2.后台接收处理
@RequestMapping(value = "/uploadFiles",method = RequestMethod.POST)
@ResponseBody
public void jsonFile(@RequestParam("uploadFile") MultipartFile uploadFile) throws Exception {
String filename = uploadFile.getOriginalFilename();//文件的名称
File files = new File("D:/"+ filename);//上传的地址
uploadFile.transferTo(files);
}