当用户选择图像时,我有此代码(请参见下文) 我想显示图像的预览。它适用于标准格式 但由于某些浏览器不支持tiff,因此如果显示图像已损坏 用户正在选择tiff图像。当我得到时,我对转换/编码图像没有问题 它来自服务器我的问题是在客户端。
The relevant javascript:
$(function () {
$(document).on('change', '#ImageData', function (evt) {
try {
var file = evt.target.files[0] || evt.srcElement.files[0]; // Read the first file from the list.
}
catch (e) {
// Something went wrong or no file was chosen -
// remove current image and return.
removeImg();
return;
}
if (isImage(file)) { // Add only valid images
var reader = new FileReader();
reader.onloadend = (function () {
return function (event) {
var img = document.getElementById('img-id');
img.src = event.target.result;
};
})(file);
reader.readAsDataURL(file); // Read in the image file as a data URL.
}
else {
removeImg(); // If there is already image remove it
}
});
});
The relevant html:
@Html.ValidationMessageFor(model => model.imageFile, "", new { @class = "text-danger" })
@Html.TextBoxFor(model => model.imageFile, new { type = "file", name ="imageFile", id = "ImageData" })
<img id='img-id' style="height: 300px; width: auto;">