我正在从服务器获取 Base64 编码的图像。
我的 Base64 响应是正确的。 在客户端我使用以下:
var blob = base64ToBlob(content, {type: 'image/jpeg'});
URL = window.URL || window.webkitURL;
var blobUrl = URL.createObjectURL(blob);
//Displaying image in div works.
var elImage = $("#dbimage");
elImage.append("<img src='data:image/jpeg;base64," + content+ " '/>");
///This doesn;t work.
return blobUrl;
var base64ToBlob = function(base64) {
var binary = atob(base64);
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
return new Blob([view], {type: 'image/jpeg'});
};
我将 blobURL 返回到 HandleBar 模板。它似乎无法识别该 Blob 。
此转换封装在 promise 中。在 HandleBar 模板中,我写的是这样的内容:
<img {{bind-attr src=image.content}} />
此 promise 存在于图像属性中。 div 中显示的图像工作正常,但 blob 中的图像无法显示。
编辑:
我正在返回 promise ,如下所示:
image : function() {
var promise = jQuery.ajax({
url: '/getattachments/?id=' + this.id + "&name=" + docname,
contentType: 'image/jpeg',
type: 'GET',
processData : false,
success: function(content) {
var blob = base64ToBlob(content, { type: 'image/jpeg'} );
URL = window.URL || window.webkitURL;
var blobUrl = URL.createObjectURL(blob);
return blobUrl;
},
error : function(content)
{
Em.Logger.info('Model:', this.id, 'has no image', err);
return '';
}
return DS.PromiseObject.create({ promise: promise });
}.property('_attachments'),
});
请帮忙。
谢谢。
最佳答案
在您提供的特定答案中存在一些小问题。
该属性必须是计算属性,而不仅仅是函数
foo: function(){
}.property() // <-------this here
此外,您使用的 Ember Data(DS 命名空间)的一部分不是 Ember 的一部分,它是一个单独的产品。这很好,但如果您要使用它,则需要包含该文件。
由于 Promise 返回的是直接文本,而不是对象,因此您需要连接到 Promise 对象的 content 属性。
<img {{bind-attr src=img.content}}/>
关于javascript - Ember - HandleBar 不显示 blob 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24109857/