<a href="javascript:;" class="file">选择文件
<input type="file" name="taskImgs" id='taskImgs' multiple="multiple">
</a>
$.ajaxFileUpload({
url : 'cases/taskPost', //提交的路径
secureuri : false, // 是否启用安全提交,默认为false
fileElementId : 'taskImgs', // file控件id
dataType : 'json',
data : param,
success : function(data, status) {
console.log(data);
alert("上传成功");
},
error : function(data, status) {
console.log(data);
alert("上传失败");
}
});
public void taskPost() throws Exception{
getFiles().size();//大小始终为1
}
getFiles().size();大小始终为1,就没办法给上传的每个文件重命名了
------------------------------------------------------------------------------------------------------
<form id="taskPublishForm" name="taskPublishForm" action="#" method="post" enctype="multipart/form-data" >
<a href="javascript:;" class="file">选择文件
<input type="file" name="taskImgs" id='taskImgs' multiple="multiple">
</a>
<input type="hidden" id="taskName" name="taskName" value="">
$("form[name='taskPublishForm").ajaxSubmit({//jquery.form.js
url:'/taskPost',
type:'post',
dataType:'json',
beforeSerialize:function(){
$("#taskName").val($("#MissionName").val());
public void taskPost() throws Exception{
MultipartParser mp = new MultipartParser(this.getRequest(), 52428800, false, false, "UTF-8");
Part part = null;
String savePath=PathKit.getWebRootPath()+"\\upload\\";//"C:\\Users\\mrl\\Documents\\upload\\";
JSONObject json = new JSONObject();
while((part=mp.readNextPart())!=null){
String name=part.getName();
if(part.isParam()){
ParamPart paramPart=(ParamPart)part;
System.out.println(name+" "+paramPart.getStringValue());;
}else if(part.isFile()){
FilePart filePart=(FilePart)part;
String fileName = filePart.getFileName();
String fileType=fileName.substring(fileName.lastIndexOf(".")+1);
String newFileName=FileUtil.instance.randomFileName()+"."+fileType;
boolean res = FileUtil.writeFile(filePart.getInputStream(), savePath, newFileName);
json.put(fileName+"_status", res);
}
}
renderJson(json);
}