I am trying to send a file(with a size of 64Kb) in base64 format to my .Net web service in node js using soap, but the result is empty. I don't know if it is because of the size of the base64 string, because when I send this string empty it responded normally.
file = req.file;
if(file){
file64 = base64_encode('public/assets/uploads/' + file.filename);
}
var args = {file: file64};
soapClient.uploadFile(args, function(err, result) {
if(err){
console.log(err);
}else{
var response = JSON.parse(result.uploadFileResult);
if(!response.response){
console.log(response.message);
}else{
//Handlers
}
}
});
When I print the result in console it shows: {}
Related
I want to send a captured image from the raspberry pie client (python) to the server (node.js).
We encoded the image as base64 and sent it back to the server using decoding the base64 as an image, but the image was broken because the file was in a different format.
Here is my code:
client.py
import base64
from PIL import Image
import os, sys
ip = ''
port = 3008
s = socket.socket()
s.connect((ip, port))
image_path = '/home/pi/TCPproject/test.jpg'
if image_path != '':
with open(image_path, "rb") as imageFile:
image_data = base64.b64encode(imageFile.read())
else:
image_data = 'cusdom_image'
s.send(image_data)
s.close()
server.js
var fs = require('fs');
var base64ToImage = require('base64-to-image');
var sockets = [];
var server = net_server.createServer(function(client) {
console.log('Client connection: ');
console.log('local = %s:%s', client.localAddress, client.localPort);
console.log('remote = %s:%s', client.remoteAddress, client.remotePort);
client.setTimeout(500);
client.setEncoding('utf8');
sockets.push(client);
var imageData;
client.on('data', function(data) {
imageData+= data;
});
client.on('end', function() {
console.log('end!')
var decoded = Buffer.from(imageData, 'base64');
fs.writeFile("test.jpg", decoded, function (err) {
if (err) throw err;
else console.log('Saved!');
});
});
client.on('error', function(err) {
console.log('Socket Error: ', JSON.stringify(err));
});
client.on('timeout', function() {
console.log('Socket Timed out');
});
});
server.listen(3008, function() {
console.log('Server listening: ' + JSON.stringify(server.address()));
server.on('close', function(){
console.log('Server Terminated');
});
server.on('error', function(err){
console.log('Server Error: ', JSON.stringify(err));
});
});
function writeData(socket, data){
var success = socket.write(data);
if (!success){
console.log("Client Send Fail");
}
}
Please let me know if encoding, decoding is wrong, or if the TCP socket communication process is wrong, or if there is another problem.
There are multiple problems with the code. On the client side:
s.send(image_data)
This might send image_data but it might only send part of image_data since send is not guaranteed to send everything. Use sendall to send everything or check the return value of send and make sure to send the rest later if not everything was sent at once.
On the server side:
var imageData;
client.on('data', function(data) {
imageData+= data;
});
client.on('end', function() {
console.log('end!')
var decoded = Buffer.from(imageData, 'base64');
If you would have a look at imageData before decoding you would see that it starts with the string undefined and only then the base64 data are following. But all of this is treated as input to the base64 decoder, leading to corrupt data. To fix this initialize imageData:
var imageData = '';
I'm working with the Microsoft Bot Framework (hosting on Azure) using Node.js, and saving files locally isn't an option. I have image data in base64 format, and I need to transfer that data into a readable stream of an image file that I can pump into Azure using their Blob Storage API (which takes a stream).
var base64ImageData; //my base64 image
//Create myStream here
//Then send to Azure
blobService.createBlockBlobFromStream('mycontainer',
nameForBlob, myStream, fileSize,
function (error, result, response){
if(!error)
console.log(response);
else
console.log(error)
});
Any help would be appreciated. I can't figure out how to decode the base64 and make a stream out of it without saving a jpg onto my disk.
Please try something like this:
var azureStorage = require('azure-storage');
var streamifier = require('streamifier');
var base64String = 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEiSURBVDhPxZIhT0NBEITnoBKBrEQgEUhISEiqKiFUYJDI/gBEc8VVY5AYHKKGBIEAVBFN+A8EUVFZUdGk/XbvGtqmpS8ImGTu5vbd7N3tPv0Pog5gzeSGB4oiqgSbCnphNbRQsKEQorbY/YCqaqwrXatl4WIJosrsfELtw3vucOFxsP4J6eRnlJnfOf3S4xk/J0hmO3kPfmE+5er+9ilSAqtoU203TGEFC7pDHcFBNvf82wxSgqAzxhPmDsbdHLtl9FZhbmDuul5AKmLUNuoDtQMH8BGeQ0OXBIckGOX1HL67ELlq6m8pBRyjbF56umEzz9KbPnXM9qBKjhhuMFsdVmKxC/ZzvCbpVW9kvRLzCeydY/9J+sx11laPXyB63/8C0gQlpj3Fc3O2WAAAAABJRU5ErkJggg==';
var blobService = azureStorage.createBlobService('account-name',
'account-key');
var buffer = Buffer.from(base64String, 'base64');
var stream = streamifier.createReadStream(buffer);
blobService.createBlockBlobFromStream('container-name', 'checked.png', stream, buffer.byteLength, function(error, response) {
if (error) {
console.log('Error!');
console.log(error);
} else {
console.log('Blob uploaded successfully!');
console.log(response);
}
});
I had to install streamifier node package to convert buffer to stream.
I am compressing a string in Python and trying to decompress it in JavaScript.
Compress a string in Python:
import zlib, base64
text = "Test string"
result = base64.b64encode(zlib.compress(text.encode("utf-8"))
In JavaScript:
const zlib = require('zlib');
var data = atob(<text encoded in base64 from Python>);
zlib.inflate(data, function(err, buffer){
if(!err){
// doing something
}
else{
console.log(err); // <-- Error: data error: incorrect header check
}
});
In JavaScript, it returns "Incorrect header check" error. How should I compress the data in Python to obtain the same value of zlib.inflate() in JavaScript?
You are passing a string to zlib.inflate in your javascript code, but it expects a Buffer instance.
var data = Buffer.from('eJwLSS0uUSguKcrMSwcAGWAEWA==', 'base64')
zlib.inflate(data, function(err, buffer) {
if (!err) {
console.log(buffer.toString());
} else {
console.log(err);
}
})
Test string
Having successfully logged in, and have got the list of documents from an envelope, I am trying to retrieve a signed document from DocuSign, using the DocuSign Node SDK, using this code:
envelopesApi.getDocument(accountId, envelopeId, documents[0].documentId, function(err, data, response) {
log.info({data: data.substring(0, 100)}, 'getDocument data');
log.info({response}, 'getDocument response');
fs.writeFile('/home/martin/downloaded.pdf', data, (err) => {
next(err);
});
});
The data variable is a string. It is not base64 encoded. The first logging statement (using the Bunyan logging module) shows that the string starts with these characters:
%PDF-1.4
%ûüýþ
4 0 obj
<<
/Parent 3 0 R
/Resources 5 0 R
/MediaBox [0.00000 0.00000 595.00000 842.00
therefore I can see that it's not base64 encoded. It seems strange to me to be holding the contents of a pdf file in a string. I was expecting a Buffer object.
When I open (in Chrome) the file that this code saved, it appears to be a valid pdf file (i.e. Chrome doesn't error saying the file is corrupt), and it has the right number of pages, but it is completely unreadable. There is no legible text at all on the page, indicating that something has got corrupted.
Looking at the EnvelopesApi.js and ApiClient.js files from the SDK I can see that it's requesting a PDF, and that there is code in the ApiClient specifically for handling PDFs - which appears to be reading from a readable stream & appending to a string.
I'm aware that there's an alternative to not use the NOde SDK, and just use the REST API directly (as per the examples in the official REST API Recipe: Getting an Envelope's Document List), but I'd like to use the SDK if possible.
Am I missing something that I should be doing with this data parameter?
See the api recipe to download a document here
Here is the sample code to download a document.
envelopesApi.getDocument(accountId, envelopeId, documentId, function (error, document, response) {
if (error) {
console.log('Error: ' + error);
return;
}
if (document) {
try {
var fs = require('fs');
var path = require('path');
// download the document pdf
var filename = accountId + '_' + envelopeId + '_' + documentId + '.pdf';
var tempFile = path.resolve(__dirname, filename);
fs.writeFile(tempFile, new Buffer(document, 'binary'), function (err) {
if (err) console.log('Error: ' + err);
});
console.log('Document ' + documentId + ' from envelope ' + envelopeId + ' has been downloaded to ' + tempFile);
} catch (ex) {
console.log('Exception: ' + ex);
}
}
});
We faced the same issue today and it was not because of the Docusign API but because we used Amazon Lambda functions; to make it work, we had to change a bit our serverless.yml file and add a binaryMediaTypes section like this:
provider:
name: aws
runtime: nodejs12.x
region: eu-west-3
profile:xxxxx
versionFunctions: false
apiGateway:
binaryMediaTypes:
- 'text/html'
Hope it will help
var https = require('follow-redirects').https;
var fs = require('fs');
var options = {
'method': 'GET',
'hostname': 'demo.docusign.net',
'path': '/restapi/v2/accounts/{account_id}/envelopes/{envelope_id}/documents/combined',
'headers': {
'X-DocuSign-Authentication': ' {"Username": "*******","Password": "******","IntegratorKey": "*******"}'
},
'maxRedirects': 20
};
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
var filename = '_combined.pdf';
var tempFile = filename;
fs.writeFile(tempFile, Buffer.from(body,"binary"), function (err) {
if (err) console.log('Error: ' + err);
});
console.log('Document from envelope has been downloaded');
});
res.on("error", function (error) {
console.error(error);
});
});
req.end();
in a Node.js app, i send an image via Socket.io to the server. in the server, i get it like this and i wanted to save it on the disk. it save's but i cant open that image and image viewer says it's damaged! file size is correct but i can't open it. is there anything that i miss in the code?
socket.on('newImage', function (data) {
var img = data.image;
var coded_img = new Buffer(img, 'base64');
fs.writeFile("out.png", coded_img,'base64', function(err) {
if (err) throw err;
console.log('File saved.')
});
});
I solved it, With this function, i recovered main image data:
decodeBase64Image = function(dataString) {
var matches = dataString.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/),
response = {};
if (matches.length !== 3) {
return new Error('Invalid input string');
}
response.type = matches[1];
response.data = new Buffer(matches[2], 'base64');
return response;
};