我尝试将图像编码为 Base64 数据字符串,但输出结果有问题。我的 Base64 字符串非常短,无法包含有效数据。如果我在测试 div 中打印它,我会得到正常且长的字符串。问题出在哪里我也不知道。我想使用我的代码来预览 Logo 。
function readURL(input, object) {
if (input.files && input.files[0])
{
var reader = new FileReader();
reader.onload = function (e)
{
$(object).attr('style', 'background-image :url("' + e.target.result + '")');
console.log(e.target.result);
}
reader.readAsDataURL(input.files[0]);
}};
这是我的职责...
$("#company_avatar").change(function(){
readURL(this, '#company_avatar_preview');});
并且这个函数正在使用中。
最佳答案
您正在获取 Base64 字符串,最好将其添加到 img src 中。你不能给出后台base64代码。
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.readAsDataURL(input.files[0]);
reader.onload = function(e) {
$('#' + '<%=ImageDisplay.ClientID%>').attr('src', e.target.result).width(256).height(96);
$('#status').html("Done loading. processed 1 files.");
};
}
}
图像显示
<input id="fileBox" name="fileBox" type="file" onchange="readURL(this);" accept="image/png, image/jpeg, image/gif" runat="server"/>
<img id="ImageDisplay" src="#" alt="#Default" runat="server"/>
<div id="status"></div>
关于javascript - Jquery 中的 Base64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941487/