AjaxFileUpload实现多文件上传

来源:互联网 发布:邪恶镰刀 数据 编辑:程序博客网 时间:2024/05/23 20:30

去年5月中旬在项目中使用过Ajax上传文件(一直都在飘,也就最近两周开始写博客了),现在要使用的时候不得不再去看过去的代码了,我是将原本的 AjaxFileUpload 单文件改成了多文件上传的,虽然不是太灵活但也能满足需求了。

使用的是 AjaxFileUpload v2.1,地址 http://www.phpletter.com/Our-Projects/AjaxFileUpload/

完成多文件上传需要改动 ajaxfileupload.js 文件,将原始的代码注释掉再改写,大约在 39行

/* var oldElement = jQuery('#' + fileElementId);var newElement = jQuery(oldElement).clone();jQuery(oldElement).attr('id', fileId);jQuery(oldElement).before(newElement);jQuery(oldElement).appendTo(form); */if(typeof(fileElementId) == 'string'){  fileElementId = [fileElementId];  }for(var i in fileElementId){  var oldElement = jQuery('#' + fileElementId[i]);  var newElement = jQuery(oldElement).clone();  jQuery(oldElement).attr('id', fileId + i);  jQuery(oldElement).before(newElement);  jQuery(oldElement).appendTo(form);  }

更改了之后,在上传参数 fileElementId 更改为数组,其余的参照官方文档

$.ajaxFileUpload({url: 'test.php',secureuri:false,//加入到数组中fileElementId:['fileone','filetwo'],dataType: 'json',data:{'width': $('#width').val(),'height': $('#height').val(),'fileone': $('#fileone').val(),'filetwo': $('#filetwo').val(),'content': $('#content').val()},success: function (data, status){if(typeof(data.error) != 'undefined'){if(data.error != ''){//返回的错误信息}else{//成功返回}}},error: function (data, status, e){//错误处理alert(e+'|date:'+data+', status:'+status);}});

间隔时间比较久,而现在需要的只是单文件上传,所以未知是否有疏漏之处。


2015-02-05 编辑

 发现了有人发布了灵活的多文件上传,贴过来 http://blog.csdn.net/itmyhome1990/article/details/36433621

0 0
原创粉丝点击