【问题标题】:upload multiple files via ajax jquery without form通过没有表单的ajax jquery上传多个文件
【发布时间】:2017-05-31 22:30:42
【问题描述】:

我有一个对话框。用户可以选择上传文件。我想通过单击 jquery 对话框中的按钮将文件上传到服务器。我尝试了很多代码,但不幸的是,我找不到解决方案。请帮助我。

我的小提琴 fiddle

jQuery:

$(function(){
 $(document).on('click','.click_link',function(){
 $("#dialog_loader").dialog("open");
$("#dialog_loader").css({'display':'show'});
return false;
});

$("#dialog_loader").dialog({resizable: false,
  height:"auto",
  modal: true,
  minWidth: 400,
  autoOpen:false,
  position: 'center top',
  buttons: {
    "Update": function() {
          var form_data = new FormData();
           $.each($("input[type='file']")[0].files, function(i, file) {
            form_data.append('file', file);
         });
        form_data.append("status", 'update');   
      $.ajax({
          url:path,
            type:'POST',
            dataType: "HTML",
            contentType : false,
            processData : false,
            data:form_data,
            success:function(msg){
               if(msg==1)
               {
                   alert(123);
               } 
            }
        });
    },
    "Approve":function(){

    },
    Cancel: function() {
      $( this ).dialog( "close" );
    }
  }
});


$(document).on('click','.add_more',function(e){
    e.preventDefault();
    var filePresent = document.getElementsByClassName('file')[document.getElementsByClassName('file').length-1].files.length;
if(filePresent >0  ){
    $(this).parent().find('#extra_file_div').find('.file_div_child').append("<br/><input name='file_uploads[]' type='file' class='multi_files file' /><button class='remove'>X</button>");
    //$(this).before("<div class='file_div_child'><input name='file_uploads[]' type='file' class='multi_files file' /><button class='remove'>X</button></div>");
  }  

});

$(document).on('change','input:file',
        function(){
            $('input:file').removeClass('multi_files');
        if ($(this).val()) {
            if($(this).parent().parent().find('.remove').length <1)
            {
                $(this).after("<button class='remove first_remove' >X</button>");
            }
            $('.add_more').show();
        } 
        else
        {
            $('.add_more').hide();

        }
    });

$(document).on('click','.remove',function(){
 var length = $('#dialog_attachments').find(".file").length; 
if(length > 1 ){
  $(this).prev('input:file').remove();
  $(this).prev('br').remove();
  $(this).remove();

 }
 else
 {
     $(".file").val('');
    $(this).remove();
    $(this).parent('.file_div_child').find('br').remove();
    $('.add_more').hide();
 }

 return false;
 }); 
 });

【问题讨论】:

    标签: ajax file-upload jquery-ui-dialog


    【解决方案1】:

    我创建了一个表单并将文件输入部分放在该表单元素中。 然后使用 jquery 每个函数,我将每个文件输入添加到 form_data 对象。

     var form_data = new FormData();
             var inputs = $(document).find("#file_form input");
            $.each(inputs, function (obj, v) {
                 $.each($(v)[0].files, function(i, file) {
                            form_data.append(v.name, file);
                    });
            });
            form_data.append("status", 'update');
    

    我已经更新了小提琴。

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多