Skip to content

Instantly share code, notes, and snippets.

@tdrozdowski
Last active December 22, 2015 11:49

Revisions

  1. tdrozdowski revised this gist Sep 6, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    <file name="image" ng-model="profilePic" accept="image/png,image/jpg,image/jpeg"/>
  2. tdrozdowski created this gist Sep 6, 2013.
    15 changes: 15 additions & 0 deletions FileDirective.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    'use strict';

    angular.module('yourApp')
    .directive 'file', [ () ->
    restrict: "E"
    template: "<input type=\"file\" />"
    replace: true
    require: "ngModel"
    link: (scope, element, attr, ctrl) ->
    listener = ->
    scope.$apply ->
    (if attr.multiple then ctrl.$setViewValue(element[0].files) else ctrl.$setViewValue(element[0].files[0]))

    element.bind "change", listener
    ]
    24 changes: 24 additions & 0 deletions FileDirective.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    'use strict';
    angular.module('yourApp').directive('file', [
    function() {
    return {
    restrict: "E",
    template: "<input type=\"file\" />",
    replace: true,
    require: "ngModel",
    link: function(scope, element, attr, ctrl) {
    var listener;
    listener = function() {
    return scope.$apply(function() {
    if (attr.multiple) {
    return ctrl.$setViewValue(element[0].files);
    } else {
    return ctrl.$setViewValue(element[0].files[0]);
    }
    });
    };
    return element.bind("change", listener);
    }
    };
    }
    ]);
    21 changes: 21 additions & 0 deletions fileInputDemo.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # to be put in a controller - the $http probably should be put into a service, simplified for this example

    $scope.uploadProfile = () ->
    file = $scope.profilePic
    config =
    method : "PUT"
    url : "/profilePic"
    headers : {'Content-Type' : undefined}
    data : { file: $scope.profilePic }
    transformRequest : (data) ->
    fd = new FormData()
    angular.forEach data, (value, key) ->
    fd.append key, value

    fd

    $http(config)
    .success (results) ->
    console.log "File Upload Results ->", results
    .error (results) ->
    console.log "File upload failed ->", results
    29 changes: 29 additions & 0 deletions fileInputDemo.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // NOTE - this is presumed to be within an exiting Controller - the $http should be put into a service for proper abstraction.

    $scope.uploadProfile = function() {
    var config, file;
    file = $scope.profilePic;
    config = {
    method: "PUT",
    url: "/profilePic",
    headers: {
    'Content-Type': void 0
    },
    data: {
    file: $scope.profilePic
    },
    transformRequest: function(data) {
    var fd;
    fd = new FormData();
    angular.forEach(data, function(value, key) {
    return fd.append(key, value);
    });
    return fd;
    }
    };
    return $http(config).success(function(results) {
    console.log("File Upload Results ->", results);
    }).error(function(results) {
    return console.log("File upload failed ->", results);
    });
    };