Help for creating this came from multiple sources:
Created
November 18, 2015 22:03
-
-
Save garrypolley/2616820361dcaf80d321 to your computer and use it in GitHub Desktop.
Simple file upload for Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<upload-file upload-url="http://google.com/"> | |
</upload-file> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UploadFile.$inject = ['$parse', '$http']; | |
function UploadFile($parse, $http) { | |
var fileToUpload; | |
return { | |
restrict: 'E', | |
scope: { | |
uploadUrl: '=' | |
}, | |
template: '<input type="file">', | |
link: function(scope, element) { | |
element.bind('change', function() { | |
var fileToUpload = element.children()[0].files[0], | |
formData = new FormData(); | |
formData.append('file', file); | |
$http.post(scope.uploadUrl, formData, { | |
transformRequest: angular.identity, | |
headers: {'Content-Type': undefined} | |
}) | |
.success(function(){ | |
console.log('Successful file upload'); | |
element.unbind('change'); | |
}) | |
.error(function(){ | |
console.log('Failed to upload file'); | |
}); | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment