0

I have written some code for capturing image using nodejs and I have used npm base64-to-image, but it's not working properly. Below is the code:

var base64Str = policyObj.image;                    
var path ='public/images/';
var optionalObj = {'fileName': 'policyimg', 'type':'png'};
base64ToImage(base64Str,path,optionalObj); 

Please let me know where I am going wrong.

2 Answers 2

1

One possible problem:

"Error: Invalid base64 string"

To debug, try replacing 1st line with: var base64Str = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";

Another:

An incorrect path as a logical error, runs code without error but not saving the image file as it should. Ie, if the path points to a non-existant directory, misspelled directory or a inaccessable directory.

To debug, try replacing 2nd line with var path ='./'; so it will save in the script directory. Note that '/' leads to no file saved, and an empty string leads to error "Missing mandatory arguments..."

Sign up to request clarification or add additional context in comments.

7 Comments

1st solution what you have mentioned is right. But the problem what i am facing is , my image is not getting supported
may be you are saving a image by wrong extesnion example saving .png file as .jpg @SRK
actually. that's not the issue. I am giving the proper extension, but i wonder there is something else is going wrong. @shivshankar
Is that a message you get from trying to open the saved file, then probably it's something bad with policyObj.image you got from somewhere. To find out what, start with console.log(typeof policyObj.image) to see if it is a string. Next, try console.log(policyObj.image). Look at the output if it starts with data:image/xxx;base64, where xxx is the image format. It's not uncommon that data-urls like this get misformatted in the header or additional citation marks, and it's possible to "repair" the string if that's the case.
Good luck! More about dataURLs, see http://dataurl.net, more on setting using file-paths in node: [shapeshed.com/writing-cross-platform-node/… paths). Also, check the source code of base64-to-image to study how it's supposed to work.
|
0
// target is path where you want to save file example /upload/filename.png
fs.writeFile(target, new Buffer(base64Str, "base64"), function (err) {
     if(err)   console.log(err);
      console.log('The file has been saved!');
});

3 Comments

i implemented your logic , but i am getting has Missing mandatory arguments base64 string and/or path string near base64ToImage
var base64Str = policyObj.image; var path ='public/images'; var optionalObj = {'fileName': 'policyimg', 'type':'png'}; base64ToImage(base64Str,path,optionalObj); fs.writeFile(path, new Buffer(base64Str, "base64"), function (err) { if(err) console.log('Error:' + err); else { console.log('The file has been saved!'); } });
Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.