Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs post base64 string as form data

I am currently receiving a file encoded as a base64 string as part of a json payload:

{
    "file":"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPGRlZmluaXRpb25zIHhtb..."
}

With that string I am supposed to post the file as multipart/form-data to a different service so I have a method like this (using request module):

var request = require('request');
var fs = require('fs');

var importFile = function(fileBase64Encoded, cb) {
    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
        if (err) {
            cb(err);
        }
        cb(null, body);
    });
    var form = r.form();
    form.append('file', decodedFile);
}

And this is currently not working. If I write file to disk and read it from there like this:

var request = require('request');
var fs = require('fs');

var importFile function(fileBase64Encoded, cb) {

    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    fs.writeFile('temp.txt', decodedFile, function (err) {
        if (err) return console.log(err);
        var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
            if (err) {
                cb(err);
            }
            cb(null, body);
        })
        var form = r.form();
        form.append('file', fs.createReadStream('temp.txt'));
    });

}

Then it works...so Is there a real way to pass the base64 string as a valid parameter to the form? (right now trying with a buffer and not working)

like image 934
jdrm Avatar asked Aug 26 '14 17:08

jdrm


People also ask

How to encode a string in Base64 Node js?

The easiest way to encode Base64 strings in Node. js is via the Buffer object. In Node. js, Buffer is a global object which means that you do not need to use require statement in order to use Buffer object in your applications.

How to decode Base64 encoded string in Nodejs?

Base64 Decoding All you need to do is create a buffer from the Base64 encoding string by using base64 as the second parameter to Buffer. from() and then decode it to the UTF-8 string by using the toString() method.

How do I display Base64 in HTML?

Complete HTML/CSS Course 2022 To display images encoded with Base64, you need to get the base64 encoded string and use the img element. This prevents the page from loading slowly and saves the web browser from additional HTTP requests. The 1st part is the Base64 encoded image.

Can Base64 string compress?

Base64 is already encoded in a way which does not suit most compression algorithms - see Why does base64-encoded data compress so poorly? for details. You will want to compress the original binary data and then base64 the compressed data, or don't bother converting to base64 at all.


1 Answers

I assume that http://localhost:8888/upload is expecting file to be ... a "file". When you pass a file stream to form.append() it already knows it's a "file". When passing a Buffer it does not.

You can, however, tell form.append() to interpret your Buffer as a "file" by passing an options object as the third argument. The options object should have a key called filename containing a string with the name of the file. Optionally, the object can also include a contentType string and knownLength integer. If contentType is not included, form.append() will try to derive the content-type from the filename.

Other than to determine contentType (when it is not explicitly passed in the options argument), the filename you specify is irrelevant (i.e. you can use whatever file name you want). Unless, of course, you intend to use the filename on the server-side code.

In your case, the following should work.

var importFile = function(fileBase64Encoded, cb) {
    var decodedFile = new Buffer(fileBase64Encoded, 'base64');
    var r = request.post('http://localhost:8888/upload', function (err, httpResponse, body) {
        if (err) {
            cb(err);
        }
        cb(null, body);
    });
    var form = r.form();
    form.append('file', decodedFile, { filename: 'temp.txt' });
}
like image 179
JME Avatar answered Oct 13 '22 13:10

JME