简体   繁体   English

Jquery中的Base64编码

[英]Base64 encoding in Jquery

I try to encode my image to base64 data string, but I have problem with output result. 我尝试将我的图像编码为base64数据字符串,但我输出结果有问题。 My base64 string is very short to has a valid data. 我的base64字符串很短,有一个有效的数据。 If I print it in test div, I have normal and long string. 如果我在测试div中打印它,我有正常和长字符串。 Where the problem I don't know. 哪里的问题我不知道。 I want to use my code for previewing 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]);
}};

This is my function... 这是我的功能......

$("#company_avatar").change(function(){
readURL(this, '#company_avatar_preview');});

And this function in use. 而且这个功能在用。

You are getting base64 string back, its better to add that to img src. 你得到base64字符串,最好把它添加到img src。 You cant give background base64 code. 你不能提供后台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.");

            };
        }
    }

ImageDisplay ImageDisplay

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM