Skip to content

Instantly share code, notes, and snippets.

@Shiti
Last active December 10, 2015 23:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shiti/4507900 to your computer and use it in GitHub Desktop.
Save Shiti/4507900 to your computer and use it in GitHub Desktop.
"use strict";
component.directive('fileupload', function () {
return {
restrict: 'E',
template: '<span>' +
'<label for="displayImg">Upload image</label>' +
'<input type="file" id="displayImg" onchange="angular.element(this).scope().setFile(this)">' +
'<button class="btn btn-primary" ng-click="uploadFile()">Save</button>'
'</span>'
replace: true,
controller: function ($scope) {
$scope.setFile = function (elem) {
$scope.inputField = elem;
$scope.file = elem.files[0];
};
$scope.uploadFile = function () {
var fd = new FormData(), xhr = new XMLHttpRequest();
fd.append("displayPic", $scope.file);
xhr.open("POST", "file/upload/path");
xhr.send(fd);
$scope.inputField.value = "";
};
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment