1.(修改一)//单个文件上传
//var oldElement = jQuery('#' + fileElementId);
//var newElement = jQuery(oldElement).clone();
//jQuery(oldElement).attr('id', fileId);
//jQuery(oldElement).before(newElement);
//jQuery(oldElement).appendTo(form); //多文件上传
for(var i in fileElementId){
var oldElement = jQuery('#' + fileElementId[i]);
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
jQuery(newElement).hide();
}
2.(修改二)
var form = jQuery('#' + formId);
jQuery(form).attr('action', s.url);
jQuery(form).attr('method', 'POST');
jQuery(form).attr('target', frameId);
if(form.encoding){
form.encoding = 'multipart/form-data';
}else{
form.enctype = 'multipart/form-data';
}
jQuery(form).submit();
//解决由于安全考虑,在IE和chrome下 ,jquery的clone并不能克隆input field的值,所以就导致克隆后新的input丢失了原来的值。
//多文件上传解决文本框消失的问题
/*var oldElement = jQuery('#jUploadFile' +id ,form);
var newElement = jQuery('#'+s.fileElementId );
jQuery(newElement).replaceWith(oldElement);
jQuery(oldElement).attr('id', s.fileElementId );*/
//解决由于安全考虑,在IE和chrome下 ,jquery的clone并不能克隆input field的值,所以就导致克隆后新的input丢失了原来的值。
for(var i in s.fileElementId){
var oldElement = jQuery('#jUploadFile' +id ,form);
var newElement = jQuery('#'+s.fileElementId[i]);
jQuery(newElement).replaceWith(oldElement);
jQuery(oldElement).attr('id',s.fileElementId[i]);
}
3.js代码
$.ajaxFileUpload({
type: "POST",
url: "regist/regist.action",
data:{'companyName':companyName,'companyShortName':re_jianjie,
'companyContactName':re_companyPerson,'companyLoginName':loginName,
'password':password,'companyContactTel':re_companyMobile,
'verifCode':verifCode,'companyTax':companyTax,
'companyAddress':companyAddress,'platformFlag':net
},
secureuri : false,//是否启用安全提交,默认为false
fileElementId:['re_localPath1','re_localPath2','re_localPath3'],//文件选择框的id属性
//fileElementId:'re_localPath1',
contentType:"text/html;charset=utf-8",
dataType:"JSON",//服务器返回的格式
cache: false,
success: function(data){
//data==="{\"message\":\"1\",\"fileName\":\"1478505365424.png\"}";(strtus中配置<result type="json"><param name="root">result</param><param name="contentType">text/html</param></result> )
var obj=eval("("+(data.substring(1,data.length-1).replace(/\\/g,""))+")");
if(obj.flag=='1'){
alert(obj.message+"请等待平台方审核,审核通过后才可以登录!")
}else{
fileName="";
$("#s_register").attr("disabled",false);
alert(obj.message);
return;
}
},
});
3.java后台
protected void reflashAjaxUploadFile(Object[] ajaxFile)
{
SimpleDateFormat fm=new SimpleDateFormat("yyyyMM");
String fp=fm.format(new Date());
if(ajaxFile != null && this._fileName !=null)
{
String [] fileNames = this._fileName.split(", ");
for(int i = 0; i<ajaxFile.length;i++)
{
File file = (File)ajaxFile[i];
File dir=new File(this._uploadFilePath+fp);
if(dir.isDirectory()){
dir.mkdir();
}
boolean existFlag = true;
if (file.isFile())
{ //验证文件上传的格式
Pattern reg=Pattern.compile("[.]jpg|png|jpeg|gif|bmp$");
Matcher matcher=reg.matcher(fileNames[i].toLowerCase());
if(!matcher.find()) {
throw new RuntimeException("文件格式不对,只能上传图片格式!");
}
while (existFlag) {
String strNewFileName =fp+String.valueOf(System.currentTimeMillis()) + this.getExtention(fileNames[i]);
if (i == 0) {
this._newFileName = strNewFileName;
} else if (i == 1) {
this._secondNewFileName = strNewFileName;
}
File targetFile = new File(this._uploadFilePath + strNewFileName);
if (!targetFile.exists()) {
if(file.length() > 2*1024*1024){
throw new RuntimeException("上传文件中,单个文件大小不允许超过2M");
}
this.copy(file, targetFile);
_lastFileName += strNewFileName + "@@";
existFlag = false;
}
}
}
}
System.out.println(this._uploadFilePath+ _lastFileName);
}
}
//var oldElement = jQuery('#' + fileElementId);
//var newElement = jQuery(oldElement).clone();
//jQuery(oldElement).attr('id', fileId);
//jQuery(oldElement).before(newElement);
//jQuery(oldElement).appendTo(form); //多文件上传
for(var i in fileElementId){
var oldElement = jQuery('#' + fileElementId[i]);
var newElement = jQuery(oldElement).clone();
jQuery(oldElement).attr('id', fileId);
jQuery(oldElement).before(newElement);
jQuery(oldElement).appendTo(form);
jQuery(newElement).hide();
}
2.(修改二)
var form = jQuery('#' + formId);
jQuery(form).attr('action', s.url);
jQuery(form).attr('method', 'POST');
jQuery(form).attr('target', frameId);
if(form.encoding){
form.encoding = 'multipart/form-data';
}else{
form.enctype = 'multipart/form-data';
}
jQuery(form).submit();
//解决由于安全考虑,在IE和chrome下 ,jquery的clone并不能克隆input field的值,所以就导致克隆后新的input丢失了原来的值。
//多文件上传解决文本框消失的问题
/*var oldElement = jQuery('#jUploadFile' +id ,form);
var newElement = jQuery('#'+s.fileElementId );
jQuery(newElement).replaceWith(oldElement);
jQuery(oldElement).attr('id', s.fileElementId );*/
//解决由于安全考虑,在IE和chrome下 ,jquery的clone并不能克隆input field的值,所以就导致克隆后新的input丢失了原来的值。
for(var i in s.fileElementId){
var oldElement = jQuery('#jUploadFile' +id ,form);
var newElement = jQuery('#'+s.fileElementId[i]);
jQuery(newElement).replaceWith(oldElement);
jQuery(oldElement).attr('id',s.fileElementId[i]);
}
3.js代码
$.ajaxFileUpload({
type: "POST",
url: "regist/regist.action",
data:{'companyName':companyName,'companyShortName':re_jianjie,
'companyContactName':re_companyPerson,'companyLoginName':loginName,
'password':password,'companyContactTel':re_companyMobile,
'verifCode':verifCode,'companyTax':companyTax,
'companyAddress':companyAddress,'platformFlag':net
},
secureuri : false,//是否启用安全提交,默认为false
fileElementId:['re_localPath1','re_localPath2','re_localPath3'],//文件选择框的id属性
//fileElementId:'re_localPath1',
contentType:"text/html;charset=utf-8",
dataType:"JSON",//服务器返回的格式
cache: false,
success: function(data){
//data==="{\"message\":\"1\",\"fileName\":\"1478505365424.png\"}";(strtus中配置<result type="json"><param name="root">result</param><param name="contentType">text/html</param></result> )
var obj=eval("("+(data.substring(1,data.length-1).replace(/\\/g,""))+")");
if(obj.flag=='1'){
alert(obj.message+"请等待平台方审核,审核通过后才可以登录!")
}else{
fileName="";
$("#s_register").attr("disabled",false);
alert(obj.message);
return;
}
},
});
3.java后台
protected void reflashAjaxUploadFile(Object[] ajaxFile)
{
SimpleDateFormat fm=new SimpleDateFormat("yyyyMM");
String fp=fm.format(new Date());
if(ajaxFile != null && this._fileName !=null)
{
String [] fileNames = this._fileName.split(", ");
for(int i = 0; i<ajaxFile.length;i++)
{
File file = (File)ajaxFile[i];
File dir=new File(this._uploadFilePath+fp);
if(dir.isDirectory()){
dir.mkdir();
}
boolean existFlag = true;
if (file.isFile())
{ //验证文件上传的格式
Pattern reg=Pattern.compile("[.]jpg|png|jpeg|gif|bmp$");
Matcher matcher=reg.matcher(fileNames[i].toLowerCase());
if(!matcher.find()) {
throw new RuntimeException("文件格式不对,只能上传图片格式!");
}
while (existFlag) {
String strNewFileName =fp+String.valueOf(System.currentTimeMillis()) + this.getExtention(fileNames[i]);
if (i == 0) {
this._newFileName = strNewFileName;
} else if (i == 1) {
this._secondNewFileName = strNewFileName;
}
File targetFile = new File(this._uploadFilePath + strNewFileName);
if (!targetFile.exists()) {
if(file.length() > 2*1024*1024){
throw new RuntimeException("上传文件中,单个文件大小不允许超过2M");
}
this.copy(file, targetFile);
_lastFileName += strNewFileName + "@@";
existFlag = false;
}
}
}
}
System.out.println(this._uploadFilePath+ _lastFileName);
}
}
楼主你好,如果用使用了multiple属性,异步上传文件时所需的fileElementId:arrId→这个id数组如何产生呢,ajaxFileUpload插件已更改