PHP AJAX Image Uploading with FormData

后端 未结 3 778
野的像风
野的像风 2020-12-16 06:42

I am relatively new to jQuery and Ajax functions, but have been working with Ajax forms over the past few days. I have come to a problem with file uploads however when tryin

3条回答
  •  生来不讨喜
    2020-12-16 07:18

    This will work for one or multiple files.

    $('input:file').on('change', function () {  
    
     var data = new FormData();
    
     //Append files infos
     jQuery.each($(this)[0].files, function(i, file) {
         data.append('file-'+i, file);
     });
    
     $.ajax({  
         url: "my_path",  
         type: "POST",  
         data: data,  
         cache: false,
         processData: false,  
         contentType: false, 
         context: this,
         success: function (msg) {
              alert(msg);
          }
      });
    });
    

    Then

    $_FILES['file-0']
    $_FILES['file-1']
    [...]
    

    But be careful that using FormData doesn't work on IE before IE10

提交回复
热议问题