在jquery中使用base64设置图像源
本文关键字:设置 图像 base64 jquery | 更新日期: 2023-09-27 17:51:05
在我的web应用程序中,我将图像存储为数据库中的字节数组,在页面中,我想使用自动完成
在下拉菜单中显示图像我使用下面的jquery代码
$(function() {
$('#userName').autocomplete({
source: function(request, response) {
$.getJSON("/Users/getData?term=" + request.term, function(data) {
response(data);
});
},
select: function(event, ui) {
$("#userName").val(ui.item.label);
return false;
},
minLength: 2,
delay: 100
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.value + item.Image + " - " + item.label + "</a>")
.appendTo(ul);
};
});
项。图像是一个字节数组,如何显示图像呢,
谢谢
标准的base64编码图像是:
<img src='data:image/jpeg;base64, LzlqLzRBQ...<!-- base64 data -->' />
所以更新你的代码:
.append("<a>" + item.value + "<img src='data:image/jpeg;base64, "+ item.Image + "' /> - " + item.label + "</a>")
[编辑]getData中的代码必须进行(服务器端)转换,即项目的结果。图像应该是base64:
Convert.ToBase64String(imagebytearray)