I have been searching for solutions to this question everywhere, but none of them seem to work for me.
The goal is to download an image from the internet using an aspx page. I want to call the aspx page from JavaScript, retrieve the data, and display it in an Image element. However, I haven't had any luck with that so far.
Here is what I am currently attempting:
System.Net.WebClient wc = new System.Net.WebClient();
byte[] data = wc.DownloadData("http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg");
Context.Response.Clear();
Context.Response.ContentType = "image/JPEG";
Context.Response.OutputStream.Write(data, 0, data.Length);
Context.Response.Flush();
Context.Response.Close();
Context.Response.End();
In JavaScript:
var url = "GetHotlink.aspx";
var tmptxt = call_genpic(url); // call server with filename and minimum layer value.
var imageObj = new Image();
imageObj.setAttribute('src', 'data:image/jpeg;base64,' + tmptxt);
document.body.appendChild(imageObj);
function call_genpic(url) {
var reqObj = createRequest();
reqObj.onreadystatechange = function () {
if (reqObj.readyState == 4) {
//callback
}
}
reqObj.open("GET", url, false);
reqObj.send(null);
var V = "";
V = reqObj.responseText;
return V;
return(reqObj.responseText);
}
Although I receive a substantial amount of data from the server when calling the aspx page, the image displayed on the page appears as a broken icon instead of Darth Vader. It seems like there might be an issue with the format or headers along the way. Any suggestions?