Problem with WordPress plugin sending base64 image data via Ajax – 400/404/500 errors

Solution:

Figured a way round it, I’ll give my solution in case anyone else has the same issue and comes across this post.

Basically I converted my base64 image data to a blob, and appended that to a newly created formData object. I found that also appending my nonce and action (amended to work with the admin-ajax way of using AJAX) to the formData helped deal with most issues on the javascript side. In the AJAX request I set processData and contentType to false.

As for the PHP side, I set a variable equal to the specific $_FILES array element I just sent. I used file_get_contents() on that variable (i.e the blob data), and wrapped that in file_put_contents() to actually write the image.

That’s the quick version. If anyone wants a more detailed explanation let me know.