Base64 encode an image from a URL on server


#1

Has anyone had any experience with trying to generate a base64 encoded string of an image from URL on the server?

Specifically, I have images stored using CollectionFS that I want to retrieve (server-side) and generate a data URI for an IMG element.

Possibly this is much simpler than I imagine.


#2

Hi, something like this :smile:

Meteor.methods({
    downloadImage: function (imageUrl) {
        //https://atmospherejs.com/froatsnook/request
        var result = request.getSync(imageUrl, {encoding: null});
        return 'data:image/png;base64,' + new Buffer(result.body).toString('base64');
    }
}) 

Demo : http://noscript.meteor.com/


#3

@ccuilla, you just need to:

finction encodeImageData: (imageData, encoding) {
  var buffer = new Buffer(imageData, encoding || 'binary');
  return buffer.toString('base64');
}

@nxcong, I guess we could use core HTTP package for calling request


#4

Thanks guys. Very helpful!


#5

@mrzafod @nxcong @ccuilla Thanks for sharing this… I am trying to do something similar on the server (see below).
The file uploads to s3 but when I upload a PDF the browser says invalid PDF.
Any ideas/direction, please ?

                                        var resultA = request.getSync(attachments[i].url, paramsA);
                                        if (resultA.response){
                                            var randomFileName = Random.id();
                                           
                                            var fileBuf = new Buffer(resultA.response.body,'binary').toString('base64')
                                            console.log("Random file:",randomFileName, resultA.response.headers["content-type"]);

                                            var params = {Bucket: 'mybucket', Key: randomFileName, Body:fileBuf ,ContentType:resultA.response.headers["content-type"] };

                                            var getParams = {Bucket: 'mybucket', Key: randomFileName};
                                            var url = s3.getSignedUrl('getObject', getParams);
                                            s3.upload(params, function(err, data) {
                                                if (err) {
                                                    console.log("Error uploading data: ", err);
                                                } else {
                                                    console.log("Successfully uploaded data to myBucket/myKey");
                                                }
                                            });
                                        }

                                       
                         

The response headers I get from “request” are:

So not sure if I have to do something special for this… eg “chunked”?


Trying to get file via HTTP then upload to S3
#6

@adamgins @mrzafod @nxcong @ccuilla
Hi, guys…
I build a web service REST API from meteor and the client is native android java. I wanna upload file from my android using rest or ddp. but until now, i havent found the solution about uploading file via ddp or using rest. Anybody wants to help me?


#7

Hi.
Can you call simle POST request to your API from Android App? If so than you can simle convert any file or any data to base64 and send in POST body.


#8

@mrzafod yeah, I can. but data is sended via parameters. I still haven’t found the way to send post with multipart or formdata. I haven’t tried to send base64 encoded via parameters. because I think it will be so long String. Is it possible?


#9

Hmmm. I gues It should be the way how to send data via body from Android. You should digg this issue first


#10

I just found your post. Solved an issue for me. Thanks for sharing!


#11

you can use this to send post with form-data.

var bodyParser = Meteor.npmRequire( 'body-parser');

// Add two middleware calls. The first attempting to parse the request body as
// JSON data and the second as URL encoded data.
Picker.middleware( bodyParser.json() );
Picker.middleware( bodyParser.urlencoded( { extended: false } ) );
var postRoutes = Picker.filter(function(req, res) {
  // you can write any logic you want.
  // but this callback does not run inside a fiber
  // at the end, you must return either true or false
  return req.method == "POST";
});