-
Notifications
You must be signed in to change notification settings - Fork 27.4k
support input[type=file] binding #1375
Comments
There is no default binding provided by angular to input type=file. But then, file input is real-only to javascript, the only benefit such a binding could help is on clearing the fileinput. (Angular also doesn't provide framework support to ajax upload of file right now I believe) |
@coli : thats not the whole truth. It would make sense to be able to catch the onchange event on input[file]. At https://github.com/lgersman/jquery.orangevolt-ampere we have exactly that scenario : file uploads via XMLHTTPRequest V2. I would love to see that feature in angularjs ! |
Has there been any progress made with binding to an input of type file? I have been searching for a solution and can't seem to find one that works... |
my solution was just Ajax instead of request or http.post Sent from my iPad On Mar 1, 2013, at 7:16 PM, "jgoldber" <notifications@github.commailto:notifications@github.com> wrote: Has there been any progress made with binding to an input of type file? I have been searching for a solution and can't seem to find one. — |
Best solution I found: http://jsfiddle.net/marcenuc/ADukg/49/. Please share if you come across a better solution. |
Yeah, this really needs looking at; being able to select & upload a file is fairly crucial to most web applications, so supporting this input is a pretty obvious missing feature. |
Bump. This needs to be added to a milestone. |
Another shout out in support of this. |
ng-model file could set value to a file from HTML5 File API, there are Flash and Silverlight polyfills for IE8/IE9 |
Yeah, either set the value to the file or to the input element (containing reference to the file) |
This solution depends on filereader being implemented: ...
.directive('file', function() {
return {
restrict: 'E',
template: '<input type="file" />',
replace: true,
require: 'ngModel',
link: function(scope, element, attr, ctrl) {
var listener = function() {
scope.$apply(function() {
attr.multiple ? ctrl.$setViewValue(element[0].files) : ctrl.$setViewValue(element[0].files[0]);
});
}
element.bind('change', listener);
}
}
}); <file name="image" ng-model="inputFile" accept="image/png,image/jpg,image/jpeg" /> You then have to do some xhr stuff yourself but I believe that this is outside the gui binding and more processing logic. and if you lack imagination: var file = $scope.inputFile;
$http.put('http://localhost:5984/demo/d06917e8d1fae1ae162ea7773c003f0b/' + file.name + '?rev=4-c10029f35a5c5ed9bd8cc31bf8589d3c', file, { headers: { 'Content-Type' : file.type } }); This works because of xhr2 (http://www.html5rocks.com/en/tutorials/file/xhr2) and please excuse the long example upload url, its a couchdb document endpoint (attachment). |
It is possible to make this work in IE8/9 by combining this flash-based FileReader Polyfill https://github.com/Jahdrien/FileReader and this pure js FormData polyfill that relies on FileReader https://github.com/francois2metz/html5-formdata. update Moxie is a better polyfill |
My solution was pretty sketchy, at least as sketchy as the jsfiddle above where the controller prototype is diddled with... In the controller: $scope.updateImage = '(' + function () {
alert('moo');
}.toString() + ')();'; Stringify my desired onchange callback and wrap it in a closure. <input type="file" name="image" onchange="{{updateImage}}" ng-model="image"> Insert it into the DOM using Angular Not exactly pretty but it works in Webkit. I didn't do any cross browser testing. |
I'm on 1.0.5 and it's still broken. We used Jgoldber's example and tweaked it a bit: This example will only work for one controller, so if you have multiple controllers, each with file inputs you'll need to make some changes. Haven't tested it on IE yet, but my guess (knowing IE) is that it won't work. |
Assuming that you just want to call a function in your scope that takes the input element as an argument, this is pretty straightforward to implement in a directive: http://jsfiddle.net/neilsarkar/vQzKJ/ (modified from the above jsfiddle, thanks @programmist) |
configuration:
usage:
|
Please do fix this soon. |
Still having problems.. in some versions it works but is anyway not supported |
Since 1.5.0 $digest has to be called if you're using onchange workaround:
|
+1 for better support on the ng-change and the input/file. Use case: as an uploader of an image, I would like to be able to edit that image (resize and crop) prior to uploading for optimal display. Implementation: browse to image (using input/file) and on the onchange event move the file to the canvas and edit with fabricjs. I can;t think of a non-on change implementation. cztomsik - I am on 1.0.7 and I cannot get your solution to work - the scope(this) function resolves fine, but the controller cannot be found, uploadFile function is listed as a member of the scope(this). Messing about, I have a slight modification on the technique, which seems to be working for me (i still don't know why I had the : < input type="file" name="file" ng-model="file" onchange="invokeUploadFile(this)"/ > var invokeUploadFile = function(that) { |
Could you provide jsfiddle/plunker?
|
+1. Need it for initiating an HTML5 file upload when a user picks a file. It's easy to work around, but I'd expect this to work. |
+1. please implement, not implementing it is totally not intuitive. |
+1 |
+1 |
1 similar comment
+1 |
this worked for me, may help angular.module('app', [])
.controller('MainCtrl', MainCtrl)
.directive('fileChange', fileChange);
function MainCtrl($scope) {
$scope.upload = function () {
// do something with the file
alert($scope.file.name);
};
}
function fileChange() {
return {
restrict: 'A',
require: 'ngModel',
scope: {
fileChange: '&'
},
link: function link(scope, element, attrs, ctrl) {
element.on('change', onChange);
scope.$on('destroy', function () {
element.off('change', onChange);
});
function onChange() {
ctrl.$setViewValue(element[0].files[0]);
scope.fileChange();
}
}
};
} <div ng-app="app" ng-controller="MainCtrl">
<input type="file" file-change="upload()" ng-model="file">
</div> |
+1 |
3 similar comments
+1 |
+1 |
+1 |
+1000000000000000000000000 |
+1 |
1 similar comment
+1 |
@dciccale can you please provide plunker example. |
+1 |
1 similar comment
+1 |
+1 There should be a directive built into the core library for this. Its pretty ridiculous |
+1 |
+1 |
+1 |
1 similar comment
+1 |
How to this in projects where $scope is never being injected into the controller. I am using laravel angular which uses GULP and ES6 way of declaring controllers. i.e.
I found a work around I believe. http://stackoverflow.com/questions/38449126/how-to-set-up-ng-file-upload-in-laravel-angular-project/38486379#38486379 |
+1 |
@ntrp @guruward @coli @lgersman @jgoldber @Siyfion My validator is considering catalougeIconName value as empty/null after i upload. |
Any updates on this? |
Any updates ? |
See #1375 (comment) |
hi trying to do a simple file upload :) but the input of type=file dont seem to bind ive tried hg-model-instant but get nothing, even upgraded to 1.0.2 and nothing
The text was updated successfully, but these errors were encountered: