2016-02-22 22 views
3

我是新来的离子和角度而现在我米试图将文件转换为Base64字符串。我正在使用cordova文件插件选择文件,文件可能是图像,pdf,文档或任何类型。我有该文件的完整路径。我怎样才能得到该文件的base64字符串?我可以使用文件路径转换为base64或者实现此目的的方式。获取文件的base64字符串离子

回答

2

phonegap-base64.这是科尔多瓦的插件和工作对我来说好。

+0

感谢Harrish;)最简单,最简单的解决方案 – aqarain

+0

它不适用于PDF文件并返回空字符串。 –

1

使用角的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> 
+0

首先,非常感谢Alex。但什么应该是参数,它需要字符串作为参数,但我有pdf/img文件路径作为参数,因此如何以及应该通过什么作为参数? – aqarain

+1

如果参数是图像,你会得到base64编码的图像,如果参数是字符串,你会得到base64编码的字符串。 –

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); 
+0

非常感谢com.ulf。但什么应该是参数,它需要字符串作为参数,但我有pdf/img文件路径作为参数,因此如何以及应该通过什么作为参数? – aqarain

+0

只有链接的答案不是很好 - 考虑从两个链接中添加详细信息,特别是针对此答案。 –

+0

@JF它,谢谢你的提示 –