I have angularjs application where user should be able to import data from text files. Before upload, I would like to read several lines from file on a client side to do some validations. I am using https://github.com/nervgh/angular-file-upload to upload file. I can upload file and do validation on a server side but files can be few megabytes big and I do not want to create unnecessary traffic if data are invalid.
Inside my controller I have following code:
$scope.validateFile = function(file){
console.log(file.name);
console.log(file.size);
var reader = new FileReader();
reader.onloadend = function(evt){
console.log(evt.target.result);
//do something with file content here
};
//var blob = file.slice(0, file.size - 1);
reader.readAsText(file);
};
When executed, console outputs file name and size. After that I got follwoing error:
TypeError: Failed to execute 'readAsText' on 'FileReader': parameter 1 is not of type 'Blob'.
When I remove comment from the following line...
var blob = file.slice(0, file.size - 1);
and change the last line to be:
reader.readAsText(blob);
this is the error from console
TypeError: undefined is not a function
It looks like that slice function is not working. Why slice() doesn't work? Is it possible to do it like this?
UPDATE:
Based on DTing's comment I found an error
My original html was
<input type="file" nv-file-select="" uploader="uploader" multiple /><br/>
...
<tr ng-repeat="item in uploader.queue">
...
ng-click="validateFile(item.file)"
I have changed last line to
ng-click="validateFile(item._file)"
Now it works.
console.log(file)
give you? – DTing Apr 3 at 9:17