Skip to content

Commit

Permalink
Display an image if detected in the response data
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Leamon authored and stiak committed Jan 20, 2011
1 parent bb3ca4e commit 66b0250
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions content/restclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ var restclient = {
var startTime = new Date().getTime();
xmlHttpRequest.onerror = function() { restclient.doErrorResponse(this); };
xmlHttpRequest.onload = function() { restclient.doResponse(this, startTime); };
// Required to handle binary (image) responses
xmlHttpRequest.overrideMimeType("text/plain; charset=x-user-defined");
xmlHttpRequest.send(requestBody);
}
catch (e) {
Expand Down Expand Up @@ -310,9 +312,15 @@ var restclient = {
restclient.addHttpHeader(key, headValue);
}
}
responseBody.value = xmlHttpRequest.responseText;

if (xmlHttpRequest.getResponseHeader("Content-Type").indexOf("json") >= 0) {

var contentType = xmlHttpRequest.getResponseHeader("Content-Type");
if (contentType.indexOf("image") >= 0) {
this.displayImage(xmlHttpRequest.responseText, contentType);
} else {
responseBody.value = xmlHttpRequest.responseText;
}

if (contentType.indexOf("json") >= 0) {
var outputDiv = document.getElementById("xmlContent");
json2xul.prettyPrintJSON(outputDiv, xmlHttpRequest.responseText);
return;
Expand All @@ -337,6 +345,29 @@ var restclient = {
}
},

displayImage: function(responseData, contentType) {
var toConvert = "";
for(i = 0; i < responseData.length; i++){
toConvert += String.fromCharCode(responseData.charCodeAt(i) & 0xff);
}
var base64encoded = btoa(toConvert);
var imgSrc = "data:" + contentType + ";base64," + base64encoded;

var hbox = document.createElement("hbox");
hbox.setAttribute("pack", "center");
hbox.setAttribute("flex", "1");

var vbox = document.createElement("vbox");
vbox.setAttribute("pack", "center");
hbox.appendChild(vbox);

var image = document.createElement("image");
image.setAttribute("src", imgSrc);
vbox.appendChild(image);

document.getElementById("xmlContent").appendChild(hbox);
},

clearRequest: function(){
var requestUrl = document.getElementById('tbRequestUrl');
var requestBody = document.getElementById('tbRequestBody');
Expand Down

0 comments on commit 66b0250

Please sign in to comment.