我正在使用jasny引导程序在页面中包含图像上传。见http://jasny.github.io/bootstrap/javascript/#fileinput

我的问题:我想将此图像保存到PouchDB之类的Javascript数据库中-因此,我认为我唯一的选择是使用Base64(http://pouchdb.com/guides/attachments.html-我将使用网络服务将该图像复制到另一个网站,所以我认为必须是base64)。但是,如何将图像转换为base64字符串格式?

最佳答案

HTML:

<input type='file' id="asd" />
<img id="img" src="" />
<div id="base"></div>


JS:

function readImage(input) {
    if ( input.files && input.files[0] ) {
        var FR= new FileReader();
        FR.onload = function(e) {
             $('#img').attr( "src", e.target.result );
             $('#base').text( e.target.result );
        };
        FR.readAsDataURL( input.files[0] );
    }
}

$("#asd").change(function(){
    readImage( this );
});

07-24 09:46
查看更多