Ajax file upload in Wordpress - can't pass FormData

Stefan

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. However, after I added it to Wordpress, as a plugin, it keeps giving me "Uncaught TypeError: Illegal invocation".

I can't afford to serialize the formdata, simply because then I won't be able to pass the file to the callback function in PHP.

JS involving FormData before ajax call:

var fd = new FormData();
var file = jQuery(this).find('input[type="file"]');
var caption = jQuery(this).find('input[name=img_caption]');
var individual_file = file[0].files[0];
fd.append("file", individual_file);
var individual_capt = caption.val();
fd.append("caption", individual_capt);

This part above is 100% correct.

Ajax call:

jQuery.ajax({
    type: 'POST',
    url: fiuajax.ajaxurl,
    data: {
        action: 'fiu_upload_file',
        security: fiuajax.security,
        data: fd,
        contentType: false,
        processData: false,
    },
    success: function(response){
        var dataObj = jQuery.parseJSON(response);
        if(dataObj.message == 'error') {
            jQuery('.fiu_validation').html('The following error occured: '+dataObj.desc);
        }
        else if(dataObj.message == 'success') {
            jQuery('.fiu_file').val('');
        }
        console.log(response);
    }
});

This is incredibly frustrating since it worked perfectly fine outside of Wordpress. I've tried de-registering Wordpress' jQuery and enqueueing the latest jQuery version, but it made no difference.

To recap:
1) Ajax/jQuery is refusing to pass a form object to PHP
2) Can't serialize the object because I need to preserve the file object
3) Script works outside of Wordpress
4) Tried updating to the newest jQuery version, no change

David

try this :

jQuery(document).on('click', '#submit', function(e){
    e.preventDefault();

    var fd = new FormData();
    var file = jQuery(document).find('input[type="file"]');
    var caption = jQuery(this).find('input[name=img_caption]');
    var individual_file = file[0].files[0];
    fd.append("file", individual_file);
    var individual_capt = caption.val();
    fd.append("caption", individual_capt);  
    fd.append('action', 'fiu_upload_file');  

    jQuery.ajax({
        type: 'POST',
        url: fiuajax.ajaxurl,
        data: fd,
        contentType: false,
        processData: false,
        success: function(response){

            console.log(response);
        }
    });
});

php

function fiu_upload_file(){

    var_dump($_FILES);
    exit();
}

add_action('wp_ajax_fiu_upload_file', 'fiu_upload_file');
add_action('wp_ajax_nopriv_fiu_upload_file', 'fiu_upload_file');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can't upload file to wordpress

From Dev

File Upload with jquery AJAX and FormData

From Dev

Wordpress AJAX File Upload

From Java

How to use FormData for AJAX file upload?

From Dev

Jquery 1.10 ajax file upload formdata issue

From Dev

Upload file using FormData and jQuery.ajax

From Dev

Can't upload a file using wordpress upload functions

From Dev

Can't retrieve formData sent through AJAX to insert new wordpress post, PHP sees values as blank

From Dev

JQuery / AJAX File Upload using FormData, file not posting

From Dev

Upload files with Ajax - FormData

From Dev

Wordpress can't upload files

From Dev

Wordpress can't upload files

From Dev

A FormData object is always empty when trying to upload a file using AJAX

From Dev

A FormData object is always empty when trying to upload a file using AJAX

From Dev

Laravel5.5 can't get ajax file upload

From Dev

AJAX image upload with FormData doesn't show image on server side

From Dev

Angular File upload formData

From Dev

Upload file without FormData

From Dev

React file upload with FormData

From Dev

jQuery file upload can not set second parameter in formdata

From Dev

Can't upload .sql file

From Dev

Can't upload a csv file

From Dev

Wordpress can't upload images due to permissions

From Dev

Can't insert FormData in mysql using Php and jquery ajax

From Dev

Upload a file on choosing file through ajax and jquery in wordpress plugin

From Dev

Can't upload image file using Wordpress REST API (Node JS)

From Dev

Wordpress can't write to file

From Dev

Wordpress can´t find file

From Dev

Cakephp form pass upload file to controller via ajax