简体   繁体   English

将图像字节保存到隐藏字段HTML5中

[英]Saving image bytes into hidden field HTML5

I am using following code to upload the image 我正在使用以下代码上传图像

$selectFile = $('<input type="file">').click();
      $selectFile.change(function (e) {
        var reader = new FileReader();
            reader.onload = function(event){
              var img = new Image();
              img.onload = function(){
                  $div.append('<figure class="images"><img src="'+ event.target.result +'" draggable="true" alt=""></figure>');
              }
              img.src = event.target.result;
            }
            reader.readAsDataURL(e.target.files[0]);

      });

When I look at the event.target.result it contains something like data:image/jpeg;base64,/9j/4AAQSkZJRgAB 当我查看event.target.result它包含类似data:image/jpeg;base64,/9j/4AAQSkZJRgAB

I am not sure if these are the image bytes? 我不确定这些是否是图像字节? I am not very much familiar with HTML 5. How do I get the image bytes so that i can store them into some hidden field. 我对HTML 5不太熟悉。如何获取图像字节,以便可以将它们存储到某些隐藏字段中。

The data after the "data:image/jpeg;base64," is a Base64 encoded version of the image. “ data:image / jpeg; base64”之后的数据是该图像的Base64编码版本。 You can convert it to a byte array using: 您可以使用以下命令将其转换为字节数组:

var byteCharacters = atob(b64Data);

But I'm not sure why you'd want to, when the Base64 is fine for rendering an image anyway with : 但是我不确定为什么要使用Base64来渲染图像时,为什么要这么做:

<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgAB...." >

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

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