Question
Here is my HTML form:
<form name="myForm" ng-submit="">
<input ng-model='file' type="file"/>
<input type="submit" value='Submit'/>
</form>
I want to upload an image from local machine and want to read the content of the uploaded file. All this I want to do using AngularJS.
When I try to print the value of $scope.file
it comes as undefined.
Answer
Some of the answers here propose using FormData()
, but unfortunately that is
a browser object not available in Internet Explorer 9 and below. If you need
to support those older browsers, you will need a backup strategy such as using
<iframe>
or Flash.
There are already many Angular.js modules to perform file uploading. These two have explicit support for older browsers:
- https://github.com/leon/angular-upload - uses iframes as a fallback
- https://github.com/danialfarid/ng-file-upload - uses FileAPI/Flash as a fallback
And some other options:
- https://github.com/nervgh/angular-file-upload/
- https://github.com/uor/angular-file
- https://github.com/twilson63/ngUpload
- https://github.com/uploadcare/angular-uploadcare
One of these should fit your project, or may give you some insight into how to code it yourself.