How to convert binary image data to URI for same image in nodejs?












0














I am using one API to get a profile photo of a user. This api gets me the binary data for a profile photo.



I need to be able to use this profile photo in form of a https://en.wikipedia.org/wiki/Data_URI_scheme



example:- json that will render an image where value of url should be URI.






{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}





How can I convert binary image data to URI for same image in nodejs ?? so i can put it as a value of URI










share|improve this question






















  • check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
    – wannadream
    Nov 13 at 4:01
















0














I am using one API to get a profile photo of a user. This api gets me the binary data for a profile photo.



I need to be able to use this profile photo in form of a https://en.wikipedia.org/wiki/Data_URI_scheme



example:- json that will render an image where value of url should be URI.






{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}





How can I convert binary image data to URI for same image in nodejs ?? so i can put it as a value of URI










share|improve this question






















  • check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
    – wannadream
    Nov 13 at 4:01














0












0








0







I am using one API to get a profile photo of a user. This api gets me the binary data for a profile photo.



I need to be able to use this profile photo in form of a https://en.wikipedia.org/wiki/Data_URI_scheme



example:- json that will render an image where value of url should be URI.






{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}





How can I convert binary image data to URI for same image in nodejs ?? so i can put it as a value of URI










share|improve this question













I am using one API to get a profile photo of a user. This api gets me the binary data for a profile photo.



I need to be able to use this profile photo in form of a https://en.wikipedia.org/wiki/Data_URI_scheme



example:- json that will render an image where value of url should be URI.






{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}





How can I convert binary image data to URI for same image in nodejs ?? so i can put it as a value of URI






{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}





{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAABfUlEQVRIS72UPU/DMBRF+69hYaJIbBUjfwFRkFCLKFH4iBQWOpeJbMBG5kzGJ+pLbOeFhKbhSkdW/O57t07dTowx/4K6OQaNjfXry9rK7IHInRuGaA011EM0n4PMrkLSNF1bzAhchEGaaTCceuegz68Pdb+NnYI2mzcay1WrazDfC0qSp06KoqCxXLW6BvOrIM0QIqcR8az5Qqz6BXFNGSqnEfHMPnWtT7Dyg57jRxPHcbnyhYeDu4SfPmYIDwm/WydICqvovlwJzvMcU2/hp09mCVbNoOhuVRn+EoaPHj6oILOs6iC36JoxdoVpIS5WfpCEhGHhbQtF3fW7MMuqeSKNLMswt4q61idY1UHL5cK0Eb467VnrE6z6Bck15+pyQdhj5RlRD3tcrLqDeMfyJ6rV2aeOT6uDF3Rze2WuF/uHuV6QZtoXXpBNPmeTT/Ab7itpq7NKyOV8fuAFAVdxWxzMds67zPaCYHY2O52eTM1Avo+OD8uTCF7IeJjJD3xajPLilAzRAAAAAElFTkSuQmCC",
"style": "person"
}
]
}






javascript node.js uri adaptive-cards adaptive-ui






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 at 3:55









Royjad

94




94












  • check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
    – wannadream
    Nov 13 at 4:01


















  • check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
    – wannadream
    Nov 13 at 4:01
















check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
– wannadream
Nov 13 at 4:01




check this: stackoverflow.com/questions/18650168/convert-blob-to-base64
– wannadream
Nov 13 at 4:01












1 Answer
1






active

oldest

votes


















0














You need to use btoa().



const imagData = 'data:image/png;base64,' + btoa('your-binary-data');


The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.



If you need to convert to Base64 you could do so using Buffer:



Buffer.from('your-binary-data').toString('base64')





share|improve this answer























  • btoa() isn't defined in nodejs. I think it works in plain javascript.
    – Royjad
    Nov 13 at 20:27










  • True, for that you need to use Buffer.
    – Narendra Mongiya
    Nov 13 at 20:37










  • I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
    – Royjad
    Nov 14 at 4:17










  • which version of node are you using? is it possible for you to test the raw binary if that is valid?
    – Narendra Mongiya
    Nov 14 at 4:21










  • Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
    – Royjad
    Nov 14 at 20:46











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273552%2fhow-to-convert-binary-image-data-to-uri-for-same-image-in-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to use btoa().



const imagData = 'data:image/png;base64,' + btoa('your-binary-data');


The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.



If you need to convert to Base64 you could do so using Buffer:



Buffer.from('your-binary-data').toString('base64')





share|improve this answer























  • btoa() isn't defined in nodejs. I think it works in plain javascript.
    – Royjad
    Nov 13 at 20:27










  • True, for that you need to use Buffer.
    – Narendra Mongiya
    Nov 13 at 20:37










  • I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
    – Royjad
    Nov 14 at 4:17










  • which version of node are you using? is it possible for you to test the raw binary if that is valid?
    – Narendra Mongiya
    Nov 14 at 4:21










  • Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
    – Royjad
    Nov 14 at 20:46
















0














You need to use btoa().



const imagData = 'data:image/png;base64,' + btoa('your-binary-data');


The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.



If you need to convert to Base64 you could do so using Buffer:



Buffer.from('your-binary-data').toString('base64')





share|improve this answer























  • btoa() isn't defined in nodejs. I think it works in plain javascript.
    – Royjad
    Nov 13 at 20:27










  • True, for that you need to use Buffer.
    – Narendra Mongiya
    Nov 13 at 20:37










  • I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
    – Royjad
    Nov 14 at 4:17










  • which version of node are you using? is it possible for you to test the raw binary if that is valid?
    – Narendra Mongiya
    Nov 14 at 4:21










  • Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
    – Royjad
    Nov 14 at 20:46














0












0








0






You need to use btoa().



const imagData = 'data:image/png;base64,' + btoa('your-binary-data');


The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.



If you need to convert to Base64 you could do so using Buffer:



Buffer.from('your-binary-data').toString('base64')





share|improve this answer














You need to use btoa().



const imagData = 'data:image/png;base64,' + btoa('your-binary-data');


The 'btoa-atob' module does not export a programmatic interface, it only provides command line utilities.



If you need to convert to Base64 you could do so using Buffer:



Buffer.from('your-binary-data').toString('base64')






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 20:36

























answered Nov 13 at 4:02









Narendra Mongiya

1,065618




1,065618












  • btoa() isn't defined in nodejs. I think it works in plain javascript.
    – Royjad
    Nov 13 at 20:27










  • True, for that you need to use Buffer.
    – Narendra Mongiya
    Nov 13 at 20:37










  • I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
    – Royjad
    Nov 14 at 4:17










  • which version of node are you using? is it possible for you to test the raw binary if that is valid?
    – Narendra Mongiya
    Nov 14 at 4:21










  • Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
    – Royjad
    Nov 14 at 20:46


















  • btoa() isn't defined in nodejs. I think it works in plain javascript.
    – Royjad
    Nov 13 at 20:27










  • True, for that you need to use Buffer.
    – Narendra Mongiya
    Nov 13 at 20:37










  • I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
    – Royjad
    Nov 14 at 4:17










  • which version of node are you using? is it possible for you to test the raw binary if that is valid?
    – Narendra Mongiya
    Nov 14 at 4:21










  • Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
    – Royjad
    Nov 14 at 20:46
















btoa() isn't defined in nodejs. I think it works in plain javascript.
– Royjad
Nov 13 at 20:27




btoa() isn't defined in nodejs. I think it works in plain javascript.
– Royjad
Nov 13 at 20:27












True, for that you need to use Buffer.
– Narendra Mongiya
Nov 13 at 20:37




True, for that you need to use Buffer.
– Narendra Mongiya
Nov 13 at 20:37












I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
– Royjad
Nov 14 at 4:17




I used following code to get URI using buffer. Body = raw image byte received from rest API. var buf = Buffer.from(body); var base64Data = buf.toString('base64') const imageURI = 'data:image/png;base64,' + base64Data; Then i used online convertor to test my URI & it didn't converted to image. I'm guessing this base64 isn't correct. I used another online converter which gives base64 using direct image upload & that works. site24x7.com/tools/datauri-to-image.html any thoughts ?
– Royjad
Nov 14 at 4:17












which version of node are you using? is it possible for you to test the raw binary if that is valid?
– Narendra Mongiya
Nov 14 at 4:21




which version of node are you using? is it possible for you to test the raw binary if that is valid?
– Narendra Mongiya
Nov 14 at 4:21












Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
– Royjad
Nov 14 at 20:46




Hi Narendra I'm on node v10.10.0 I am using following API to get binary. I confirmed it used REST client Postman & it does gives me image back. In nodejs app it shows me raw bytes which I pass it to Buffer. developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/…
– Royjad
Nov 14 at 20:46


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273552%2fhow-to-convert-binary-image-data-to-uri-for-same-image-in-nodejs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)