[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

convert image to base 64 for an AJAX function

jrough

5/10/2016 1:13:00 AM

I'm trying to get an image from the img on google drive, then convert it to base64 and then output the callback to innerHtml of the span of the first div. I try to pass in the toDataURL function to HTTP request but I get this error.

sample.js:6 Uncaught ReferenceError: toDataURL is not definedgetImage1Data @ sample.js:6(anonymous function) @ page.html:23
Navigated to http://localhost:8888/mydir/page.html
---

var image2Src = null;

function getImage1Data(){
var image1Src;
var xhttp = new XMLHttpRequest();
xhttp.open("Get", toDataURL('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh...) , false);

xhttp.send(null);
image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png', 200x200 px
}




function toDataUrl(url){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(base64Img);
callback(dataURL);
canvas = null;
};
img.src = url;
}

I'm not sure I get this function. The dataurl should output to here in the HTTP requrest image1Src = xmlhttp.responseText; THANKS.
8 Answers

Thomas 'PointedEars' Lahn

5/10/2016 3:27:00 AM

0

JRough wrote:
^^^^^^
Your *real* name belongs in your â??Fromâ?.

> sample.js:6 Uncaught ReferenceError: toDataURL is not definedgetImage1Data
> @ sample.js:6(anonymous function) @ page.html:23 Navigated to
> http://localhost:8888/mydir/page.html ---
>
> var image2Src = null;
>
> function getImage1Data(){
> var image1Src;
> var xhttp = new XMLHttpRequest();
> xhttp.open("Get",
>
toDataURL('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh...)
^^^^^^^^^
> , false);
^^^^^ synchronous
> xhttp.send(null);
> image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png',
> 200x200 px
> }
>
>
>
>
> function toDataUrl(url){
^^^^^^^^^
> var img = new Image();
> img.crossOrigin = 'Anonymous';
> img.onload = function(){
> var canvas = document.createElement('CANVAS');
> var ctx = canvas.getContext('2d');
> var dataURL;
> canvas.height = this.height;
> canvas.width = this.width;
> ctx.drawImage(this, 0, 0);
> dataURL = canvas.toDataURL(base64Img);
^^^^^^^ undeclared variable
> callback(dataURL);
^^^^^^^^ ?
> canvas = null;
> };
> img.src = url;
> }
>
> I'm not sure I get this function. The dataurl should output to here in
> the HTTP requrest image1Src = xmlhttp.responseText; THANKS.

This cannot work:

1. Identifiers are *case-sensitive*: it is â??toDataUrlâ?, not â??toDataURLâ?.
I recommend using â??toDataURIâ? as function name instead â?? those are
â??data:â? _URIs_, not URLs (regardless what the Canvas API might say).
<https://en.wikipedia.org/wiki/Data_URI_... p.

2. You are making a synchronously handled HTTP request (which you should
not; this blocks everything in the tab or window â?? you should get a
â??deprecatedâ? warning in the console), but image loading is still
*asynchronous*, and you are not waiting with the request until the
image has been loaded. You are not returning anything else from
toDataUrl(), so once you have fixed the other problems, you are making
an invalid request to â??undefinedâ?.

3. Probably the currently undefined â??callbackâ? in â??img.onloadâ? is to
fix the problem. You have to make the HTTP request in the function
that is referred to by what you *pass* for â??callbackâ? to toDataUrl().
IOW, â??callbackâ? should be a named parameter of toDataUrl().

4. You have not thought this through: you attempt to make an *HTTP*
request to a resource defined by a *â??data:â?* URI.

Next time, use JSHint and a debugger.

And *always* develop in strict mode, using

"use strict";

on top of functions (or only the function on the top of the scope chain â??
called, but not called by other functions), to catch mistakes early (in
recent runtime environments).

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

5/10/2016 3:42:00 AM

0

JRough wrote:
^^^^^^
Your *real* name belongs in your â??Fromâ?.

> sample.js:6 Uncaught ReferenceError: toDataURL is not definedgetImage1Data
> @ sample.js:6(anonymous function) @ page.html:23 Navigated to
> http://localhost:8888/mydir/page.html ---
>
> var image2Src = null;
>
> function getImage1Data(){
> var image1Src;
> var xhttp = new XMLHttpRequest();
> xhttp.open("Get",
>
toDataURL('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh...)
^^^^^^^^^
> , false);
^^^^^ synchronous
> xhttp.send(null);
> image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png',
> 200x200 px
> }
>
>
>
>
> function toDataUrl(url){
^^^^^^^^^
> var img = new Image();
> img.crossOrigin = 'Anonymous';
> img.onload = function(){
> var canvas = document.createElement('CANVAS');
> var ctx = canvas.getContext('2d');
> var dataURL;
> canvas.height = this.height;
> canvas.width = this.width;
> ctx.drawImage(this, 0, 0);
> dataURL = canvas.toDataURL(base64Img);
> callback(dataURL);
^^^^^^^^ ?
> canvas = null;
> };
> img.src = url;
> }
>
> I'm not sure I get this function. The dataurl should output to here in
> the HTTP requrest image1Src = xmlhttp.responseText; THANKS.

This cannot work:

1. Identifiers are *case-sensitive*: it is â??toDataUrlâ?, not â??toDataURLâ?.
I recommend using â??toDataURIâ? as function name instead â?? those are
â??data:â? _URIs_, not URLs (regardless what the Canvas API might say).
<https://en.wikipedia.org/wiki/Data_URI_... p.

2. You are making a synchronously handled HTTP request (which you should
not; this blocks everything in the tab or window â?? you should get a
â??deprecatedâ? warning in the console), but image loading is still
*asynchronous*, and you are not waiting with the request until the
image has been loaded. You are not returning anything else from
toDataUrl(), so once you have fixed the other problems, you are making
an invalid request to â??undefinedâ?.

3. Probably the currently undefined â??callbackâ? in â??img.onloadâ? is to
fix the problem. You have to make the HTTP request in the function
that is referred to by what you *pass* for â??callbackâ? to toDataUrl().
IOW, â??callbackâ? should be a named parameter of toDataUrl().

4. You have not thought this through: you attempt to make an *HTTP*
request to a resource defined by a *â??data:â?* URI.

Next time, use JSHint and a debugger.

And *always* develop in strict mode, using

"use strict";

on top of functions (or only the function on the top of the scope chain â??
called, but not called by other functions), to catch mistakes early (in
recent runtime environments).

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

John Harris

5/10/2016 8:03:00 AM

0

On Tue, 10 May 2016 05:42:14 +0200, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>JRough wrote:
>^^^^^^
>Your *real* name belongs in your ?From?.
<snip>

Now explain why
From: JRough <janis.rough@gmail.com>
isn't suitable according to your rules.

John

jrough

5/10/2016 9:52:00 PM

0

On Monday, May 9, 2016 at 8:42:19 PM UTC-7, Thomas 'PointedEars' Lahn wrote:
> JRough wrote:
> ^^^^^^
> Your *real* name belongs in your "From".
>
> > sample.js:6 Uncaught ReferenceError: toDataURL is not definedgetImage1Data
> > @ sample.js:6(anonymous function) @ page.html:23 Navigated to
> > http://localhost:8888/mydir/page.html ---
> >
> > var image2Src = null;
> >
> > function getImage1Data(){
> > var image1Src;
> > var xhttp = new XMLHttpRequest();
> > xhttp.open("Get",
> >
> toDataURL('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh...)
> ^^^^^^^^^
> > , false);
> ^^^^^ synchronous
> > xhttp.send(null);
> > image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png',
> > 200x200 px
> > }
> >
> >
> >
> >
> > function toDataUrl(url){
> ^^^^^^^^^
> > var img = new Image();
> > img.crossOrigin = 'Anonymous';
> > img.onload = function(){
> > var canvas = document.createElement('CANVAS');
> > var ctx = canvas.getContext('2d');
> > var dataURL;
> > canvas.height = this.height;
> > canvas.width = this.width;
> > ctx.drawImage(this, 0, 0);
> > dataURL = canvas.toDataURL(base64Img);
> > callback(dataURL);
> ^^^^^^^^ ?
> > canvas = null;
> > };
> > img.src = url;
> > }
> >
> > I'm not sure I get this function. The dataurl should output to here in
> > the HTTP requrest image1Src = xmlhttp.responseText; THANKS.
>
> This cannot work:
>
> 1. Identifiers are *case-sensitive*: it is "toDataUrl", not "toDataURL".
> I recommend using "toDataURI" as function name instead - those are
> "data:" _URIs_, not URLs (regardless what the Canvas API might say).
> <https://en.wikipedia.org/wiki/Data_URI_... p.
>
> 2. You are making a synchronously handled HTTP request (which you should
> not; this blocks everything in the tab or window - you should get a
> "deprecated" warning in the console), but image loading is still
> *asynchronous*, and you are not waiting with the request until the
> image has been loaded. You are not returning anything else from
> toDataUrl(), so once you have fixed the other problems, you are making
> an invalid request to "undefined".
>
> 3. Probably the currently undefined "callback" in "img.onload" is to
> fix the problem. You have to make the HTTP request in the function
> that is referred to by what you *pass* for "callback" to toDataUrl().
> IOW, "callback" should be a named parameter of toDataUrl().
>
> 4. You have not thought this through: you attempt to make an *HTTP*
> request to a resource defined by a *"data:"* URI.
>
> Next time, use JSHint and a debugger.
>
> And *always* develop in strict mode, using
>
> "use strict";
>
> on top of functions (or only the function on the top of the scope chain -
> called, but not called by other functions), to catch mistakes early (in
> recent runtime environments).
>
> --
> PointedEars
> FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
> Please do not cc me. / Bitte keine Kopien per E-Mail.

I now get the error on line 12 where I send the request:
xhttp.send(null);
and on line 62 where I call the first function:
if (event.target == buttons[0] ){
getImage1Data();
}
if (event.target== buttons[1] ) {
getImage2Data();
}
};
It still says undefined, not found
----
sample.js:12 GET http://localhost:8888/mydir/undefined 404 (Not Found)getImage1Data @ sample.js:12element.onclick @ sample.js:62
myFirstImg.png:1 GET http://localhost:8888/Dropbox/myFirstImg.png 404 (Not Found)Load image (async)toDataUrl @ sample.js:50getImage1Data @ sample.js:8element.onclick @ sample.js:62
myLastImg.png:1 GET http://localhost:8888/Dropbox/myLastImg.png 404 (Not Found)Load image (async)toDataUrl @ sample.js:50getImage2Data @ sample.js:26element.onclick @ sample.js:65

--------
"use strict";

var image2Src = null;

function getImage1Data(){
var image1Src;
var xhttp = new XMLHttpRequest();
xhttp.open("Get", toDataUrl('../../Dropbox/myFirstImg.png', function(base64Img){

}) , true);

xhttp.send(null);
// image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png', 200x200 px
image1Src = xhttp.responseText;
}


function getImage2Data(){

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200)
getSrc(xhttp.responseText);
}
xhttp.open("Get", toDataUrl('../../Dropbox/myLastImg.png',function(base64Img){

}) , true);
xhttp.send(null);
}

function getSrc(text){
image2Src = text; //Assume src = 'myLastImg.png', 10x10 px
}

function toDataUrl(url){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataUrl;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataUrl = canvas.toDataUrl();
callback(dataUrl);
canvas = null;
};
img.src = url;
}

Thomas 'PointedEars' Lahn

5/11/2016 12:07:00 AM

0

JRough wrote:
^^^^^^

> [full quote]

Learn to quote.

> I now get the error on line 12 where I send the request:
> xhttp.send(null);
> and on line 62 where I call the first function:
> if (event.target == buttons[0] ){
> getImage1Data();
> }
> if (event.target== buttons[1] ) {
> getImage2Data();
> }
> };
> It still says undefined, not found
>
> ----
> sample.js:12 GET http://localhost:8888/mydir/undefined 404 (Not
> Found)

AISB.

<http://www.catb.org/esr/faqs/smart-question...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

jrough

5/11/2016 2:26:00 AM

0

On Monday, May 9, 2016 at 8:42:19 PM UTC-7, Thomas 'PointedEars' Lahn wrote:
> JRough wrote:
> ^^^^^^
> Your *real* name belongs in your “From”.
>
> > sample.js:6 Uncaught ReferenceError: toDataURL is not definedgetImage1Data
> > @ sample.js:6(anonymous function) @ page.html:23 Navigated to
> > http://localhost:8888/mydir/page.html ---
> >
> > var image2Src = null;
> >
> > function getImage1Data(){
> > var image1Src;
> > var xhttp = new XMLHttpRequest();
> > xhttp.open("Get",
> >
> toDataURL('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh...)
> ^^^^^^^^^
> > , false);
> ^^^^^ synchronous
> > xhttp.send(null);
> > image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png',
> > 200x200 px
> > }
> >
> >
> >
> >
> > function toDataUrl(url){
> ^^^^^^^^^
> > var img = new Image();
> > img.crossOrigin = 'Anonymous';
> > img.onload = function(){
> > var canvas = document.createElement('CANVAS');
> > var ctx = canvas.getContext('2d');
> > var dataURL;
> > canvas.height = this.height;
> > canvas.width = this.width;
> > ctx.drawImage(this, 0, 0);
> > dataURL = canvas.toDataURL(base64Img);
> > callback(dataURL);
> ^^^^^^^^ ?
> > canvas = null;
> > };
> > img.src = url;
> > }
> >
> > I'm not sure I get this function. The dataurl should output to here in
> > the HTTP requrest image1Src = xmlhttp.responseText; THANKS.
>
> This cannot work:
>
> 1. Identifiers are *case-sensitive*: it is “toDataUrl”, not “toDataURL”.
> I recommend using “toDataURI” as function name instead – those are
> “data:” _URIs_, not URLs (regardless what the Canvas API might say).
> <https://en.wikipedia.org/wiki/Data_URI_... p.
>
> 2. You are making a synchronously handled HTTP request (which you should
> not; this blocks everything in the tab or window – you should get a
> “deprecated” warning in the console), but image loading is still
> *asynchronous*, and you are not waiting with the request until the
> image has been loaded. You are not returning anything else from
> toDataUrl(), so once you have fixed the other problems, you are making
> an invalid request to “undefined”.
>
> 3. Probably the currently undefined “callback” in “img.onload” is to
> fix the problem. You have to make the HTTP request in the function
> that is referred to by what you *pass* for “callback” to toDataUrl().
> IOW, “callback” should be a named parameter of toDataUrl().
>
> 4. You have not thought this through: you attempt to make an *HTTP*
> request to a resource defined by a *“data:”* URI.
>
> Next time, use JSHint and a debugger.
>
> And *always* develop in strict mode, using
>
> "use strict";
>
> on top of functions (or only the function on the top of the scope chain –
> called, but not called by other functions), to catch mistakes early (in
> recent runtime environments).
>
> --
> PointedEars
> FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
> Please do not cc me. / Bitte keine Kopien per E-Mail.
Thanks for the above info. It was helpful except now I can't test it to see if it is fixed.

I switched the server to dropbox. I am getting a CORS error.
sample.js:8 GET http://127.0.0.1:61426... 404 (Not Found)getImage1Data @ sample.js:8element.onclick @ sample.js:54
sample.js:8 GET http://127.0.0.1:61426... 404 (Not Found)getImage1Data @ sample.js:8element.onclick @ sample.js:54
page.html:1 Image from origin 'https://www.dropbo... has been blocked from loading by Cross-Origin Resource Sharing policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0..0.1:... is therefore not allowed access.

I made the xhr a cross domain request. Is there a way to add a fake header? Dropbox can't add a CORS header. It says the server must add the header..

"use strict";

var image2Src = null;

function getImage1Data(){
var image1Src;
// var xhttp = new XMLHttpRequest();
var xhttp = new XDomainRequest();
xhttp.open("Get", toDataUrl('https://drive.google.com/file/d/0Bzw8PpdNiQ6ELUdlNVdhUkw1OENlUmpDVFAxYmhrNG5LRlhn/view?usp=sh..., function(base64Img){

}) , true);

xhttp.send(null);
// image1Src = xmlhttp.responseText; //Assume src = 'myFirstImg.png', 200x200 px
image1Src = xhttp.responseText;
}


function getImage2Data(){

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200)
getSrc(xhttp.responseText);
}
xhttp.open("Get", toDataUrl('https://drive.google.com/file/d/0Bzw8PpdNiQ6EQS1Ha1JsOHl6UzQ/view?usp=sh...
,function(base64Img){

}) , true);
xhttp.send(null);
}

function getSrc(text){
image2Src = text; //Assume src = 'myLastImg.png', 10x10 px
}

function toDataUrl(url){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataUrl;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataUrl = canvas.toDataUrl();
callback(dataUrl);
canvas = null;
};
img.src = url;
}
function add(type) {
//Create an input type dynamically.
var element = document.createElement(type);
//Assign different attributes to the element.
element.type = type;
element.value = type; // Really? You want the default value to be the type string?
element.name = type; // And the name too?
element.onclick = function(event) { // Note this is a function
var buttons = document.getElementsByTagName('button');
if (event.target == buttons[0] ){
getImage1Data();
}
if (event.target== buttons[1] ) {
getImage2Data();
}
};

var foo = document.getElementById("btnDiv");
//Append the element in page (in span).
foo.appendChild(element);
}


----

jrough

5/12/2016 1:11:00 AM

0

On Tuesday, May 10, 2016 at 1:03:13 AM UTC-7, John Harris wrote:
> On Tue, 10 May 2016 05:42:14 +0200, Thomas 'PointedEars' Lahn
> <PointedEars@web.de> wrote:
>
> >JRough wrote:
> >^^^^^^
> >Your *real* name belongs in your “From”.
> <snip>
>
> Now explain why
> From: JRough <janis.rough@gmail.com>
> isn't suitable according to your rules.
>
> John

I made the suggested fixes and I am still getting the error undefined:
Navigated to http://localhost/html5test/page.html
sample.js:11 GET http://localhost/html5test/undefined 404 (Not Found)getImage1Data @ sample.js:11element.onclick @ sample.js:61
sample.js:44 Uncaught TypeError: canvas.toDataURI is not a function

Is there any more suggestions?

"use strict";
var image2Src = null;

function getImage1Data(){
var image1Src ;
var xhttp = new XMLHttpRequest();
xhttp.open("Get", toDataURI('/html5test/myFirstImage.png',function(base64Img){}) , true);



xhttp.send(null);
image1Src = xhttp.responseText; //Assume src = 'myFirstImg.png', 200x200 px//

}


function getImage2Data(){

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200)
getSrc(xhttp.responseText);
};
xhttp.open("Get",toDataURI('/html5test/myLastImage.png',function(base64Img){}), true);
xhttp.send(null);
}

function getSrc(text){
image2Src = text; //Assume src = 'myLastImg.png', 10x10 px
}

function toDataURI(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURI(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}

function add(type) {
//Create an input type dynamically.
var element = document.createElement(type);
//Assign different attributes to the element.
element.type = type;
element.value = type; // Really? You want the default value to be the type string?
element.name = type; // And the name too?
element.onclick = function(event) { // Note this is a function
var buttons = document.getElementsByTagName('button');
if (event.target == buttons[0] ){
getImage1Data();
}
if (event.target== buttons[1] ) {
getImage2Data();
}
};
var foo = document.getElementById("btnDiv");
//Append the element in page (in span).
foo.appendChild(element);
}

jrough

5/12/2016 6:57:00 PM

0

On Tuesday, May 10, 2016 at 5:07:26 PM UTC-7, Thomas 'PointedEars' Lahn wrote:
> JRough wrote:
> ^^^^^^
>
> > [full quote]
>
> Learn to quote.
>
> > I now get the error on line 12 where I send the request:
> > xhttp.send(null);
> > and on line 62 where I call the first function:
> > if (event.target == buttons[0] ){
> > getImage1Data();
> > }
> > if (event.target== buttons[1] ) {
> > getImage2Data();
> > }
> > };
> > It still says undefined, not found
> >
> > ----
> > sample.js:12 GET http://localhost:8888/mydir/undefined 404 (Not
> > Found)
>
> AISB.
>
> <http://www.catb.org/esr/faqs/smart-question...
>
> --
> PointedEars
> FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
> Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
> Please do not cc me. / Bitte keine Kopien per E-Mail.
I enabled JSHint and JSLint in my IDE. Thanks,
I I found an easier conversion function. It looks like it should work. I have a link of the files..
ERROR MSG
https://www.dropbox.com/sh/ky1clj8n0b5xzuc/AACig7JDAQgHQf1ZIRTH...
This is the error:
VM46 sample.js:27GET http://localhost/html5test/null 404 (Not Found)getImage2Data @ VM46 sample.js:27element.onclick @ VM46 sample.js:85
VM46 sample.js:24null
It looks like it is creating a blank image?


var image2Src = null;

function getImage1Data(){
var image1Src='myFirstImage.png';
var xhttp = new XMLHttpRequest();
xhttp.open("Get", image1Src, true);
xhttp.send(null);
image1Src = xhttp.responseText; //Assume src = 'myFirstImg.png', 200x200 px//
console.log(image1Src);
// getDataUri(image1Src, function(image1Src){console.log(dataUri) });
}


function getImage2Data(){
// var image2Src;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(xhttp.readyState == 4 && xhttp.status == 200)
image2Src = (xhttp.responseText);
console.log(image2Src);
};
xhttp.open("Get", image2Src, true);
xhttp.send(null);
getDataUri(image2Src, function(image2Src){console.log(image2Src) });
}



function getDataUri(url, callback) {
var image = new Image();

image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
canvas.getContext('2d').drawImage(this, 0, 0);
// Get raw image data
callback(canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, ''));

// ... or get as Data URI
// callback(canvas.toDataURL('image/png'));
};

image.src = url;
}

/*function toDataURI(url, callback, outputFormat){
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURI(outputFormat);
callback();
canvas = null;
};
img.src = url;
}*/


function add(type) {
//Create an input type dynamically.
var element = document.createElement(type);
//Assign different attributes to the element.
element.type = type;
element.value = type; // Really? You want the default value to be the type string?
element.name = type; // And the name too?
element.onclick = function(event) { // Note this is a function
var buttons = document.getElementsByTagName('button');
if (event.target == buttons[0] ){
getImage1Data();

}
if (event.target== buttons[1] ) {
getImage2Data();


}
};
var foo = document.getElementById("btnDiv");
//Append the element in page (in span).
foo.appendChild(element);
}

function putLeft(){
document.getElementsByClassName('leftDiv').getElementsByTagName('span').innerHTML = getImage1Data();

}