2

Is there a way to get name, path and size of selected file in input field using angularJS, before uploading it?

<input type="file" ng-model="fileContent" on-read-file="showContent($fileContent)" />

$scope.showContent = function($fileContent){

    $scope.content = $fileContent;

};

Can anyone help to solve this please?

Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
darkman
  • 169
  • 1
  • 3
  • 14

2 Answers2

6

The HTML5 File API will give you a File object for each file that you're attempting to upload. This File object will have a size and name property which will give you the file size in bytes and the name of the file.

There's no property for the physical path to the file on the users machine, though.

You can read more about this on MDN: https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications

More information on the File object here: https://developer.mozilla.org/en-US/docs/Web/API/File

Here's a working example: http://jsfiddle.net/fmeLz9cd/

Given an input of type file with an id fileSelected, here's an example of accessing the properties through the File API:

 $('#fileSelected').on('change', function (evt) {
    var files = $(evt.currentTarget).get(0).files;

    if(files.length > 0) {
        $('#fileName').text(files[0].name);
        $('#fileSize').text(files[0].size);
        $('#filePath').text($('#fileSelected').val());
    }
});

Update

Since you've requested an AngularJS specific example, here's the same code working in an angular app:

http://jsfiddle.net/vyc6jq84/1/

<div ng-app="fileDemo">
    <input type="file" fd-input />
</div>

var app = angular.module('fileDemo', []);

app.directive('fdInput', [function () {
    return {
        link: function (scope, element, attrs) {
            element.on('change', function  (evt) {
                var files = evt.target.files;
                console.log(files[0].name);
                console.log(files[0].size);
            });
        }
    }
}]);
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • thank you for your response, but when i add id attribute, and i try to read value of the input file i get his path : console.log($('#fileSelected').val); – darkman Dec 01 '14 at 13:44
  • If you look at the link provided, you'll see that you need to access the `files` collection. If you're using jQuery that means you'll probably need to do `$('#fileSelected').get(0).files[int]` – Jamie Dixon Dec 01 '14 at 13:46
  • thank you Jamie, it work in this manner, but i want to do it with angularJS, if you can help me, thank you – darkman Dec 01 '14 at 14:07
0

You cannot, unless the server helps. Angular is running in your browser, and cannot read the filesystem of the server or of the browser's computer. If you want to get a file, you need the server to implement something that will do so.

Now, if it is a static file, and already is served up by the server, then you could read it via $http.

$http.get(filename).success(function(data){
  // data contains the file content
});

But it may be interpreted based on the file type, etc. And this entirely presumes the file is already being served by the server.

deitch
  • 14,019
  • 14
  • 68
  • 96
  • than you for your answer, but i dont submit my file to server, i juste want read a csv file, get his content, display them in html table, after save the name of this file and his path in the database (using server side with jee) – darkman Dec 01 '14 at 14:13