Ajax file upload in Wordpress - can't pass FormData

前端 未结 5 887
失恋的感觉
失恋的感觉 2021-01-02 07:13

I\'ve made a script which uses $.ajax and FormData to pass two form objects to PHP. One form object is a text and the other is a file. It worked well as a stand-alone script

5条回答
  •  误落风尘
    2021-01-02 07:56

    here some modified code in case you have multiple data and multiple files

    var fd = new FormData();
    var data = jQuery('#yourformID').serializeArray();
    jQuery.each(data,function(key,input){
        fd.append(input.name,input.value);
    });
    var file = jQuery(document).find('input[type="file"]');
    jQuery.each(jQuery(file), function(i, obj) {
        jQuery.each(obj.files,function(j,file){
            fd.append('files[' + j + ']', file);
        })
    });
    fd.append('action', 'fiu_upload_file');
    

提交回复
热议问题