Loading a file issues

hi, i have the following code

 <input type="file" ng-model-instant id="inputFile"                  onchange="angular.element(this).scope().setFiles(this.files)" />

  .controller('ChatsCtrl', function($scope, Friends, $http, $cordovaFile) {
    $scope.setFiles = function(files) {        
    $scope.filePath = files[0].name;
    $scope.info = files[0];

    $http.get(files[0].name).success(function(response){
        Friends.setNewText(response);
    });
    /*$cordovaFile.readFile(files[0]).then(function(result) {
        $scope.info = result;
    }, function(err) {
        $scope.info = err;
    });*/
};

I have a few questions :

  1. the file input seems to return just the fileName (and not the full path) both on Android and in WebView (that’s prob a security feature). How am i supposed to load it when i dont know the full path?
    files[0].toString() does not have anything about containing folder or absolute location, just name, size, last modified.

  2. right now i get around issue #1 by placing the file to load in the www/ folder so $http loads it just fine. I also noticed that http would load it fine if i package the file inside the .apk in the same www/ folder. That’s fine for testing right now but is no go in the long term. Is there anywhere else that http would work - meaning outside the .apk ?

  3. i am assuming the answer to issue #2 is NO WHERE. So i want to get my code working in both WebView and Android. I can check using :
    http://ionicframework.com/docs/api/utility/ionic.Platform/
    whether I am on WebView and use http
    OR
    Android and use cordovaFile right? Would that work?

thanks in advance!

Try this

$scope.setFiles = function(element)
{ 
  $scope.$apply(function(scope) {
    var file = element.files[0];
    var reader = new FileReader();
    reader.onload = function(e) {
      // data:image/png;base64,...
      $scope.info = e.target.result;
    };
    reader.readAsDataURL(file);
  });
  ...
}

as suggested changed setFiles to :
$scope.setFiles = function(files)
{
$scope.$apply(function() {
var file = files[0];
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
};
reader.readAsDataURL(file);
});
}

produced in console :
data:text/csv;base64,IlRyaXAjIiwiVHJpcCBOYW1lIiwiVHJpcCBEZXN0aW5hdGlvbiIsIk…ggIFZlc3NlbGwiLCI1NzM2ODA5MzIxIiwiV0VCIiwxNjU4MTgwLCJZIiwiIiwiIiwwLCIiDQo=

so that looks like the file metadata NOT the file content?

this works!
$scope.setFiles = function(files)
{
$scope.$apply(function() {
var file = files[0];
var reader = new FileReader();
reader.onload = function(e) {
console.log(e.target.result);
};
reader.readAsText(file);
});
}

  1. I tested it in WebView(works) but does this work in Android as well??
  2. when i print the file , there is still containing folder information so not sure how this would work generally? (anywhere but www/ )

thanks again

just for every one’s benefit - the above worked in both env - webView and Android. thanks joseadrian

After running above code I got some junk output in the console :frowning:
How can I get the file path?

Hi, I have the same problem.

I need the full path of the file to copy in a folder I created in only application, but all I found so far is C: \ fakepath \ file.doc