将文件路径转换为实际字节?

时间:2015-05-15 20:39:28

标签: angularjs cordova ionic-framework

我有以下路径

"file:///var/mobile/Containers/Data/Application/92C91B4A-B7F9-40F8-B8AD-646DA0607942/Library/NoCloud/Z7sY4cdv_photo_001.jpg"

如何将此文件路径转换为文件的实际二进制表示形式?我已尝试以各种可想象的方式使用文件阅读器,但继续获取错误代码。致谢

所以我期望的结果是将字节放入javaScript对象,基本上将图像分配给对象。

1 个答案:

答案 0 :(得分:0)

试试:

   $scope.getFileBinary = function(fileUri){
    var xhr = new XMLHttpRequest();
        xhr.open("GET", fileUri, true);
      //xhr.responseType = 'blob';
        xhr.responseType = 'arraybuffer';
        xhr.onload = function(e) {
            var uInt8Array = new Uint8Array(this.response);
            if (this.status == 200 || this.status === 0) {
               // Note: .response instead of .responseText
               var blob = new Blob([this.response], {
                   type: 'image/png'
               });

             var reader = new FileReader();
                 reader.onload = function() {
                   // this gets read of the mime-type data header
                    var actual_contents = reader.result.slice(reader.result.indexOf(',') + 1);

                     d.resolve(actual_contents );
                  };
             reader.readAsDataURL(blob);
          }
  };
  xhr.send();

}