<form id="imgUploadForm" class="form-horizontal js-form col-md-10">
<label class="col-md-2 control-label">附件:</label>
<div class="input-group col-md-10">
<!--<input class="form-control" readonly="readonly" name="FILE_PATH"/>-->
<input id="js-input-file" name="filename" class="js-fileupload" accept="image/*" type="file" multiple/>
</div>
</form>
1、form 提交
<iframe name="ifr" id="ifr" style="height: 0;width: 0"></iframe>
var fileObj = that.$("#js-input-file");
var fileName = fileObj.val();
if (fileName == "") {
ngc.info('请先选择文件');
return;
}
ngc.progress("上传中");
var url = ngc.getContext() + "/fileUpload.spj?method=picStoreUpload&resTypeId=" + that.options.resTypeId + "&resId=" + that.options.resId + "&len=1";
var form = that.$("#imgUploadForm");
form.attr("method", "post");
form.attr("enctype", "multipart/form-data");
form.attr("target", "ifr");
form.attr("action", url);
form.submit();
$("#ifr").load(function () {
clearTimeout(that.options.time);
that.options.time = setTimeout(function(){
var result = $($("#ifr")[0].contentWindow.document.body).text();
ngc.progress();
if (result.indexOf("fail") > -1) {
ngc.info("上传失败");
} else {
ngc.info("上传成功");
that.queryImg();
}
$("#imgUploadForm")[0].reset();
},500)
})
2、jquery-form插件提交 https://github.com/jquery-form/form
if(!$("#js-input-file")[0].value) {
ngc.info("请选择至少一个文件");
return;
}
ngc.progress("上传中");
var url =ngc.getContext() + "/fileUpload.spj?method=picStoreUpload&resTypeId="+this.options.resTypeId+"&resId="+this.options.resId+"&len=1";
var options = {
url : url,
type: 'POST',
success : function(data){
ngc.progress();
ngc.info("上传成功");
that.queryImg();
},
error : function(){
ngc.progress();
ngc.info("上传失败");
$("#imgUploadForm")[0].reset();
},
resetForm : true
};
$("#imgUploadForm").ajaxSubmit(options);
后台
public void picStoreUpload(HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
List<String> picIds = new ArrayList<String>();
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
String resTypeId = request.getParameter("resTypeId") == null ? "0" : request.getParameter("resTypeId");
String resId = request.getParameter("resId") == null ? "0" : request.getParameter("resId");
int len = Integer.parseInt(request.getParameter("resId") == null ? "0" : request.getParameter("len"));
List<MultipartFile> imgFile = multipartRequest.getFiles("filename");
for(MultipartFile multipartFile:imgFile){
String picId = propertyDataBizService.savePhotoAndVideo(resTypeId, resId, 0, (CommonsMultipartFile)multipartFile);
picIds.add(picId);
}
out.write(picIds.toString());
out.flush();
out.close();
}