我想在我的页面中设置一个图像src,其中包含我从ajax调用响应中收到的图像,但是当我使用响应设置图像src时,我得到一个损坏的图像图标。
下面的是ajax调用
buildscript {
//repositories { jcenter() }
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
错误
答案 0 :(得分:1)
找到我的问题的解决方案
https://stackoverflow.com/a/25371174/3518278
必须添加mime-type
mimeType: "text/plain; charset=x-user-defined",
和
$("#img_carousel").attr('src', 'data:image/jpeg;base64,' + base64Encode(data));
function base64Encode(str) {
var CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var out = "", i = 0, len = str.length, c1, c2, c3;
while (i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if (i == len) {
out += CHARS.charAt(c1 >> 2);
out += CHARS.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if (i == len) {
out += CHARS.charAt(c1 >> 2);
out += CHARS.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += CHARS.charAt((c2 & 0xF) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += CHARS.charAt(c1 >> 2);
out += CHARS.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
out += CHARS.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
out += CHARS.charAt(c3 & 0x3F);
}
return out;
}