Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7945435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:51:52+00:00 2026-06-04T00:51:52+00:00

I need to be able to send an image and some form fields from

  • 0

I need to be able to send an image and some form fields from a client side canvas element to a PHP script, ending up in $_POST and $_FILES. When I send it like this:

<script type="text/javascript">
var img = canvas.toDataURL("image/png");
...
ajax.setRequestHeader('Content-Type', "multipart/form-data; boundary=" + boundary_str);
var request_body = boundary + '\n' 
+ 'Content-Disposition: form-data; name="formfield"' + '\n' 
+ '\n' 
+ formfield + '\n' 
+ '\n' 
+ boundary + '\n'
+ 'Content-Disposition: form-data; name="async-upload"; filename="' 
+ "ajax_test64_2.png" + '"' + '\n' 
+ 'Content-Type: image/png' + '\n' 
+ '\n' 
+ img
+ '\n' 
+ boundary;
ajax.send(request_body);
</script>

$_POST and $_FILES both come back populated, but the image data in $_FILES still needs decoding like this:

$loc = $_FILES['async-upload']['tmp_name'];
$file = fopen($loc, 'rb');
$contents = fread($file, filesize($loc));
fclose($file);
$filteredData=substr($contents, strpos($contents, ",")+1);
$unencodedData=base64_decode($filteredData);

…in order to save it as a readable PNG. This isn’t an option as I’m trying to pass the image to WordPress’s media_handle_upload() function, which requires an index to $_FILES pointing to a readable image. I also can’t decode, save and alter ‘tmp_name’ accordingly, as it falls foul of security checks.

So, I found this:
http://www.webtoolkit.info/javascript-base64.html
and tried to do the decode on the client side:

img_split = img.split(",",2)[1];
img_decoded = Base64.decode( img_split );

but for some reason I still don’t end up with a readable file when it gets to the PHP.
So the question is: “Why?” or “What am I doing wrong?” or “Is this even possible?” 🙂

Any help very much appreciated!

Thanks,
Kane

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:51:54+00:00Added an answer on June 4, 2026 at 12:51 am

    Building on Nathan’s excellent answer, I was able to finnagle it so that it is still going through jQuery.ajax. Just add this to the ajax request:

                xhr: function () {
                    var myXHR = new XMLHttpRequest();
                    if (myXHR.sendAsBinary == undefined) {
                        myXHR.legacySend = myXHR.send;
                        myXHR.sendAsBinary = function (string) {
                            var bytes = Array.prototype.map.call(string, function (c) {
                                return c.charCodeAt(0) & 0xff;
                            });
                            this.legacySend(new Uint8Array(bytes).buffer);
                        };
                    }
                    myXHR.send = myXHR.sendAsBinary;
                    return myXHR;
                },
    

    Basically, you just return back an xhr object that is overriden so that “send” means “sendAsBinary”. Then jQuery does the right thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to periodically send email alerts to subscribed users. PHP
I need to send an image as in form of PDF file in my
I need to be able to send the content type text/xml to Firefox and
For my application, I need to be able to send an std::vector<std::string> over a
I need to be able to compile a visual studio 2005 c++ project from
I need to be able to store some information about the user and I
i need to be able to make calls from an activity. so here is
Due to Facebook api restrictions for iOS, I need to be able to send
I want the user to be able to send an email from inside my
I need to be able to send a http get request to a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.