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)