treat blobs as binary, render images w/o image url
This commit is contained in:
@@ -651,7 +651,8 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
|
||||
|
||||
// puts the response data in UI
|
||||
showStatus: function(response) {
|
||||
var url, content;
|
||||
console.log(response.headers);
|
||||
var url, content, blob;
|
||||
if (response.content === undefined) {
|
||||
content = response.data;
|
||||
url = response.url;
|
||||
@@ -660,7 +661,9 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
|
||||
url = response.request.url;
|
||||
}
|
||||
var headers = response.headers;
|
||||
content = jQuery.trim(content);
|
||||
if(typeof content === 'string') {
|
||||
content = jQuery.trim(content);
|
||||
}
|
||||
|
||||
// if server is nice, and sends content-type back, we can use it
|
||||
var contentType = null;
|
||||
@@ -685,6 +688,37 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
|
||||
pre = $('<pre class="json" />').append(code);
|
||||
|
||||
// JSON
|
||||
} else if (headers['Content-Disposition'] && (/attachment/).test(headers['Content-Disposition']) ||
|
||||
headers['content-disposition'] && (/attachment/).test(headers['content-disposition']) ||
|
||||
headers['Content-Description'] && (/File Transfer/).test(headers['Content-Description']) ||
|
||||
headers['content-description'] && (/File Transfer/).test(headers['content-description'])) {
|
||||
|
||||
if ('Blob' in window) {
|
||||
var type = contentType || 'text/html';
|
||||
blob = new Blob([content], {type: type});
|
||||
|
||||
var a = document.createElement('a');
|
||||
var href = window.URL.createObjectURL(blob);
|
||||
var fileName = response.url.substr(response.url.lastIndexOf('/') + 1);
|
||||
var download = [type, fileName, href].join(':');
|
||||
|
||||
// Use filename from response header
|
||||
var disposition = headers['content-disposition'] || headers['Content-Disposition'];
|
||||
if(typeof disposition !== 'undefined') {
|
||||
var responseFilename = /filename=([^;]*);?/.exec(disposition);
|
||||
if(responseFilename !== null && responseFilename.length > 1) {
|
||||
download = responseFilename[1];
|
||||
}
|
||||
}
|
||||
|
||||
a.setAttribute('href', href);
|
||||
a.setAttribute('download', download);
|
||||
a.innerText = 'Download ' + fileName;
|
||||
|
||||
pre = $('<div/>').append(a);
|
||||
} else {
|
||||
pre = $('<pre class="json" />').append('Download headers detected but your browser does not support downloading binary via XHR (Blob).');
|
||||
}
|
||||
} else if (contentType === 'application/json' || /\+json$/.test(contentType)) {
|
||||
var json = null;
|
||||
try {
|
||||
@@ -710,49 +744,23 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
|
||||
code = $('<code />').text(content);
|
||||
pre = $('<pre class="plain" />').append(code);
|
||||
|
||||
|
||||
// Image
|
||||
} else if (/^image\//.test(contentType)) {
|
||||
pre = $('<img>').attr('src', url);
|
||||
//pre = $('<img>').attr('src', url);
|
||||
|
||||
var binary = '';
|
||||
var bytes = new Uint8Array( content );
|
||||
var len = bytes.byteLength;
|
||||
for (var i = 0; i < len; i++) {
|
||||
binary += String.fromCharCode( bytes[ i ] );
|
||||
}
|
||||
|
||||
pre = $('<img>').attr( 'src', 'data:' + contentType + ';base64,' + btoa(binary));
|
||||
// Audio
|
||||
} else if (/^audio\//.test(contentType) && supportsAudioPlayback(contentType)) {
|
||||
pre = $('<audio controls>').append($('<source>').attr('src', url).attr('type', contentType));
|
||||
|
||||
// Download
|
||||
} else if (headers['Content-Disposition'] && (/attachment/).test(headers['Content-Disposition']) ||
|
||||
headers['content-disposition'] && (/attachment/).test(headers['content-disposition']) ||
|
||||
headers['Content-Description'] && (/File Transfer/).test(headers['Content-Description']) ||
|
||||
headers['content-description'] && (/File Transfer/).test(headers['content-description'])) {
|
||||
|
||||
if ('Blob' in window) {
|
||||
var type = contentType || 'text/html';
|
||||
var blob = new Blob([content], {type: type});
|
||||
var a = document.createElement('a');
|
||||
var href = window.URL.createObjectURL(blob);
|
||||
var fileName = response.url.substr(response.url.lastIndexOf('/') + 1);
|
||||
var download = [type, fileName, href].join(':');
|
||||
|
||||
// Use filename from response header
|
||||
var disposition = headers['content-disposition'] || headers['Content-Disposition'];
|
||||
if(typeof disposition !== 'undefined') {
|
||||
var responseFilename = /filename=([^;]*);?/.exec(disposition);
|
||||
if(responseFilename !== null && responseFilename.length > 1) {
|
||||
download = responseFilename[1];
|
||||
}
|
||||
}
|
||||
|
||||
a.setAttribute('href', href);
|
||||
a.setAttribute('download', download);
|
||||
a.innerText = 'Download ' + fileName;
|
||||
|
||||
pre = $('<div/>').append(a);
|
||||
} else {
|
||||
pre = $('<pre class="json" />').append('Download headers detected but your browser does not support downloading binary via XHR (Blob).');
|
||||
}
|
||||
|
||||
// Location header based redirect download
|
||||
} else if(headers.location || headers.Location) {
|
||||
// Location header based redirect download
|
||||
window.location = response.url;
|
||||
|
||||
// Anything else (CORS)
|
||||
|
||||
Reference in New Issue
Block a user