angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

support input[type=file] binding

Open guruward opened this issue 14 years ago • 145 comments

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

guruward avatar Sep 18 '12 00:09 guruward

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 avatar Sep 18 '12 16:09 coli

@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. Therefore we implemented our own directive to catch input[file] change events to get fileuploads done without page reloading.

I would love to see that feature in angularjs !

lgersman avatar Sep 28 '12 14:09 lgersman

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...

jgoldber avatar Mar 02 '13 02:03 jgoldber

my solution was just Ajax instead of request or http.post

Sent from my iPad

On Mar 1, 2013, at 7:16 PM, "jgoldber" <[email protected]mailto:[email protected]> 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.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1375#issuecomment-14321221.

guruward avatar Mar 02 '13 02:03 guruward

Best solution I found: http://jsfiddle.net/marcenuc/ADukg/49/. Please share if you come across a better solution.

jgoldber avatar Mar 02 '13 20:03 jgoldber

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.

Siyfion avatar Mar 13 '13 17:03 Siyfion

Bump. This needs to be added to a milestone.

andrewrk avatar Mar 20 '13 16:03 andrewrk

Another shout out in support of this.

aravinda0 avatar Mar 24 '13 10:03 aravinda0

ng-model file could set value to a file from HTML5 File API, there are Flash and Silverlight polyfills for IE8/IE9

glebm avatar Mar 29 '13 17:03 glebm

Yeah, either set the value to the file or to the input element (containing reference to the file)

coli avatar Mar 29 '13 19:03 coli

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).

bjconlan avatar Mar 31 '13 12:03 bjconlan

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

glebm avatar Mar 31 '13 14:03 glebm

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.

jvanalst avatar Apr 19 '13 18:04 jvanalst

I'm on 1.0.5 and it's still broken. We used Jgoldber's example and tweaked it a bit: http://jsfiddle.net/ADukg/2589/

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.

programmist avatar May 01 '13 23:05 programmist

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)

neilsarkar avatar Jul 18 '13 14:07 neilsarkar

'use strict';

function filesModelDirective(){
  return {
    controller: function($parse, $element, $attrs, $scope){
      var exp = $parse($attrs.filesModel);

      $element.on('change', function(){
        exp.assign($scope, this.files);
        $scope.$apply();
      });
    }
  };
}

configuration:

angular.module(...).directive('filesModel', filesModelDirective)

usage:

<input type="file" files-model="someObject.files" multiple>

cztomsik avatar Aug 01 '13 12:08 cztomsik

Please do fix this soon.

bana-io avatar Aug 14 '13 09:08 bana-io

Still having problems.. in some versions it works but is anyway not supported

ntrp avatar Aug 14 '13 10:08 ntrp

Since 1.5.0 $digest has to be called if you're using onchange workaround:

<input type="file" onchange="angular.element(this).scope().myController.uploadFile(this.files[0]); angular.element(this).scope().$digest();">

cztomsik avatar Aug 27 '13 12:08 cztomsik

+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) { angular.element(that).scope().uploadFile(that) angular.element(that).scope().$digest(); }

tb01923 avatar Aug 29 '13 16:08 tb01923