使用ajaxSubmit方法实现多文件上传(异步)

前端之家收集整理的这篇文章主要介绍了使用ajaxSubmit方法实现多文件上传(异步)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、上传按钮

  1. <button onclick="toImport()" class="btn green">
  2. 导入<i class="icon-upload-alt"></i>
  3. </button>

2、文件选择弹出层和添加组件的HTML代码


  1. <div id="import_modal" class="modal hide fade">
  2. <form
  3. action="${pageContext.request.contextPath }/**/importFile!impXLS.do"
  4. id="ajax_form" class="form-horizontal">
  5. <div class="modal-header">
  6. <button type="button" class="close cancelUpload"
  7. data-dismiss="modal" aria-hidden="true"></button>
  8.  
  9. <h3>
  10. 文件导入
  11.  
  12. </h3>
  13. </div>
  14.  
  15. <div class="modal-body form">
  16. <div class="alert alert-error hide">
  17. <button class="close" data-dismiss="alert"></button>
  18. <span> </span>
  19. </div>
  20. <div class="control-group">
  21. <label class="control-label">
  22. </label>
  23. <div class="controls">
  24. <span style="color:red">*注:上传文件个数不能超过5个</span>
  25. </div>
  26. </div>
  27.  
  28. <div class="control-group">
  29. <label class="control-label">
  30. 选择文件
  31. </label>
  32. <div class="controls">
  33. <div class="fileupload fileupload-new" data-provides="fileupload">
  34. <div class="input-append">
  35. <div class="uneditable-input">
  36. <i class="icon-file fileupload-exists"></i>
  37. <span class="fileupload-preview"></span>
  38. </div>
  39. <span class="btn btn-file"> <span class="fileupload-new">浏览...</span>
  40. <span class="fileupload-exists">修改...</span> <input
  41. type="file" class="default" name="importFile" /> <input
  42. type="hidden" name="flag" value="${flag }" /> </span>
  43. <button type="button" class="btn green fileupload-append"
  44. style="text-align: right">
  45. 新增
  46. </button>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52.  
  53. <div class="modal-footer">
  54. <button type="button" onclick="ajaxImp()" class="btn red">
  55. 确定
  56. </button>
  57. <button type="button" data-dismiss="modal" class="btn cancelUpload">
  58. 取消
  59. </button>
  60. </div>
  61. </form>
  62. </div>
  63. <!-- 添加 文档上传组件 start-->
  64. <div class="control-group clone" style="display: none">
  65. <label class="control-label">
  66. 选择文件
  67. </label>
  68. <div class="controls">
  69. <div class="fileupload fileupload-new" data-provides="fileupload">
  70. <div class="input-append">
  71. <div class="uneditable-input">
  72. <i class="icon-file fileupload-exists"></i>
  73. <span class="fileupload-preview"></span>
  74. </div>
  75. <span class="btn btn-file"> <span class="fileupload-new">浏览...</span>
  76. <span class="fileupload-exists">修改...</span> <input type="file"
  77. class="default" name="importFile" /> <input type="hidden"
  78. name="flag" value="${flag }" /> </span>
  79. <span onClick="javascript:toRemoveFile(this);" class="btn red">移除</span>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. <!-- end -->

3、遮罩层代码
  1. <div id="loading" class="modal hide fade">
  2. <div class="modal-body">
  3. <p>
  4. 数据导入中,请等待...
  5. <img src="${pageContext.request.contextPath }/image/select2-spinner.gif" />
  6. </p>
  7. </div>
  8. </div>

4、添加/移除文件上传框 限制文件上传个数
  1. $(".fileupload-append").click(function(){
  2. if($(".form .control-group").length<=5){
  3. var fileuploadModule = $(".clone").clone(true);
  4. fileuploadModule.show();
  5. fileuploadModule.removeClass("clone");
  6. fileuploadModule.addClass("append");
  7. $(".form").append(fileuploadModule);
  8. }else{
  9. $error.hide();
  10. $("span",$error).html("可上传文件的个数不能超过5个...");
  11. $error.show();
  12. }
  13. });
  14. //移除文件上传框和文件
  15. $(".cancelUpload").click(function (){
  16. $(".append").remove();
  17. });

5、异步导入文件function ajaxImp(){
  1. function ajaxImp(){
  2. $("#ajax_form").ajaxSubmit({
  3. //clearForm:true,resetForm:true,dataType:'json',type:'post',beforeSubmit:function(formData,jqForm,options){
  4. var $error = $('.alert-error',jqForm);
  5. var flag = true;
  6. //遍历form中的导入框,判空并验证文件的格式
  7. $(".fileupload-preview",jqForm).each(function (){
  8. var fileName = $(this).text();
  9. $error.hide();
  10. if(App.isEmpty(fileName)){
  11. $("span",$error).html("请选择XLS格式文件进行导入...");
  12. //$error.show();
  13. flag = false;
  14. return false;
  15. }else if(fileName.lastIndexOf("\.")==-1||fileName.substring(fileName.lastIndexOf("\.")+1).toLowerCase()!='xls'){
  16. $("span",$error).html("导入的文件后缀,必须为XLS...");
  17. //$error.show();
  18. flag = false;
  19. return false;
  20. }
  21. });
  22. if(flag==false){
  23. $error.show();//显示错误信息
  24. return false;
  25. }
  26. $("#import_modal").modal('hide');//导入文件隐藏
  27. //导入加载框显示
  28. $("#loading").modal({
  29. backdrop:"static",keyboard:false,show:true
  30. });
  31.  
  32. return true;
  33. },success:function(data,statusText){
  34. if(statusText='success'){
  35. $("#loading").modal('hide');
  36. notification(data.headerMessage,data.message);
  37. <span style="color:#FF0000;">oTable.fnDraw(); //列表信息使用Datatables来显示</span>
  38. $(".append").remove();
  39. }
  40. }
  41. });
  42. }
  43. //提醒对话框
  44. function notification(title,message){
  45. $.gritter.add({
  46. title: title,text: message,time: 2000,sticky: false
  47. });
  48. }

6、Java代码:ImportFileAction(注意要添加相应的get和set方法)

  1. public class ImportFileAction extends BaseAction{
  2. private File[] importFile;
  3. private String[] importFileFileName;
  4. public void impXLS(){
  5. if (!StringUtil.isEmpty(importFile) && importFile.length > 0) {
  6. for (int i = 0; i < importFile.length; i++) {
  7. File impFile = importFile[i];
  8. //……文件内容读取(略)
  9. }
  10. }
  11. }
  12. }
7、效果


8、用到的js、css、gif及部分代码可点击这里http://download.csdn.net/detail/zdx1515888659/7657895下载

猜你在找的Ajax相关文章