首页 文章

获取离子文件的base64字符串

提问于
浏览
3

我是离子和角度的新手,现在我正在尝试将文件转换为base64字符串 . 我正在使用cordova文件插件选择文件,文件可以是image,pdf,doc或任何类型 . 我有该文件的完整路径 . 如何获取该文件的base64字符串?我可以使用文件路径转换为base64或实现此目的的方法是什么 .

4 回答

  • 0

    phonegap-base64.这是cordova插件,也为我工作 .

  • 2

    使用angular-base64插件 . 链接在这里:https://github.com/ninjatronic/angular-base64 .
    它很容易使用 . 在模块中注入依赖关系:

    angular
        .module('myApp', ['base64'])
        .controller('myController', ['$base64', '$scope',function($base64, $scope) {
    
                $scope.encoded = $base64.encode('a string');
                $scope.decoded = $base64.decode('YSBzdHJpbmc=');
        }]);
    

    并且,请记住在index.html中包含引用 .

    <script src="bower_components/angular-base64/angular-base64.js"></script>
    
  • 1

    你可以看看这个:

    如何使用它:

    angular
        .module('myApp', ['base64'])
        .controller('myController', [
    
            '$base64', '$scope', 
            function($base64, $scope) {
    
                $scope.encoded = $base64.encode('a string');
                $scope.decoded = $base64.decode('YSBzdHJpbmc=');
        }]);
    

    你可以这样做:

    var fileReader = new FileReader();
        fileReader.onload = function(e)
        {
            // e.target.result contains the "string" of your file
        };
        fileReader.readAsText(yourfile);
    
  • 1

    尝试

    file.toString('base64');
    

    您也可以尝试阅读这些,因为它基本上是一个基于JS的框架

    Base64 encoding and decoding in client-side Javascript

相关问题