如何在javascript中将字节数组转换为图像
我想使用Javascript将字节数组转换为图像,以便在前端显示它。 我正在使用blob将一个图像插入mysql数据库。图像在保存前已转换为字节数组 我使用json数组检索所有表值,并使用javascript显示。在json数组中,我也将此图像添加为bytearray 我能够检索其他值,但无法将此字节数组显示为图像 如何将这个字节数组转换为javascript中的图像,以便使用html显示它 上述代码:如何在javascript中将字节数组转换为图像,javascript,html,angularjs,Javascript,Html,Angularjs,我想使用Javascript将字节数组转换为图像,以便在前端显示它。 我正在使用blob将一个图像插入mysql数据库。图像在保存前已转换为字节数组 我使用json数组检索所有表值,并使用javascript显示。在json数组中,我也将此图像添加为bytearray 我能够检索其他值,但无法将此字节数组显示为图像 如何将这个字节数组转换为javascript中的图像,以便使用html显示它 上述代码: $scope.uploadFile = function (input) {
$scope.uploadFile = function (input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
//Sets the Old Image to new New Image
$('#itemImage').attr('src', e.target.result);
//Create a canvas and draw image on Client Side to get the byte[] equivalent
var canvas = document.createElement("canvas");
var imageElement = document.createElement("img");
imageElement.setAttribute('src', e.target.result);
canvas.width = imageElement.width;
canvas.height = imageElement.height;
var context = canvas.getContext("2d");
context.drawImage(imageElement, 0, 0);
var base64Image = canvas.toDataURL("image/jpeg");
//Removes the Data Type Prefix
//And set the view model to the new value
$scope.itemImage = base64Image.replace(/data:image\/jpeg;base64,/g, '');
}
//Renders Image on Page
reader.readAsDataURL(input.files[0]);
}
};
如果图像较小,您可以将其编码在base 64中,并将其作为img标记的src。如果图像较小,您可以将其编码在base 64中,并将其作为img标记的src。有;是什么让你的问题与众不同?Java!=JavaScript,请不要标记垃圾邮件;是什么让你的问题与众不同?Java!=JavaScript,请不要标记垃圾邮件。