Uploading base64 encoded image via ajax..

2,026 views
Skip to first unread message

brez

unread,
Jul 27, 2011, 1:58:39 PM7/27/11
to nodejs
Im uploading an image that's taken from a flash movie (the camera
control) like this:

$.ajax({
url: '/smilexhr',
data: 'img='+base64encoded,
type: 'POST',
});

and then trying to decode and save:

app.post('/upload', function(req, res) {
var file = new Buffer(req.body.img, 'base64')
ws = fs.writeFile('./testuploaded.jpg', file, function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
});

It saves something but its not a jpg - am I missing something
obvious? Any suggestions would be great..

Thanks

brez

unread,
Jul 27, 2011, 1:59:49 PM7/27/11
to nodejs
Edit

        $.ajax({
                url: '/upload',
                data: 'img='+base64encoded,
                type: 'POST',
        });

mscdex

unread,
Jul 27, 2011, 2:03:16 PM7/27/11
to nodejs
On Jul 27, 1:58 pm, brez <jbres...@gmail.com> wrote:
> It saves something but its not a jpg - am I missing something
> obvious?    Any suggestions would be great..

Have you tried saving the base64 data to a file and using the command-
line base64 decoder or decoding it from another language? This way we
can tell if it's a client-side encoding or a node decoding issue.

Ben Noordhuis

unread,
Jul 27, 2011, 2:27:23 PM7/27/11
to nod...@googlegroups.com
On Wed, Jul 27, 2011 at 19:58, brez <jbre...@gmail.com> wrote:
>        $.ajax({
>                url: '/smilexhr',
>                data: 'img='+base64encoded,
>                type: 'POST',
>        });

Base64-encoded data may contain '+' characters. If sent as
application/x-www-form-urlencoded, those get interpreted as spaces.

brez

unread,
Jul 28, 2011, 1:12:12 PM7/28/11
to nodejs
Yea you might be onto something - I saved the file as encoded base64
then ran:

$ openssl enc -d -base64 -in encoded.jpg -out decoded.jpg

But the decoded.jpg is 0K - this suggests a client side encoding
issue correct?

brez

unread,
Jul 28, 2011, 1:13:02 PM7/28/11
to nodejs
Right what I saved from the client *has spaces - which content-type
should I be using?

Thanks

On Jul 27, 2:27 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

brez

unread,
Jul 28, 2011, 1:13:50 PM7/28/11
to nodejs



> But the decoded.jpg is 0K

'zero' K

brez

unread,
Jul 28, 2011, 2:20:04 PM7/28/11
to nodejs
I set the content-type to image/jpeg and I can see it getting sent out
but on the server the req.body is undefined.. ? Seems like that
should be there - any thoughts? I tried using req.on('data' and
req.on('end but neither seem to ever fire - thanks

brez

unread,
Jul 28, 2011, 3:10:06 PM7/28/11
to nodejs
Actually found part of the problem - the encoded string I was getting
didn't have any line breaks (base64 requires a max of 76 characters
per line) so that was part of it - other question about processing
though still remains.. sorry new to all this, but should I be
processing the file inline like that (see above) or do I risk not
getting a complete request and should really be processing it on some
kind of request end handler (though I'm not entirely sure that is) -
thanks for your help

brez

unread,
Jul 29, 2011, 2:46:58 PM7/29/11
to nodejs



> getting a complete request and should really be processing it on some
> kind of request end handler (though I'm not entirely sure that is) -

Turns out this was because I was using connect bodyParser() - it sets
them for a POST and are not available - potential gotcha for anyone
trying to understand why they can't set 'end' / 'data' handlers using
express (which uses connect of course).
Reply all
Reply to author
Forward
0 new messages