Skip to content

Instantly share code, notes, and snippets.

@mchayapol
Created May 17, 2016 08:55
Show Gist options
  • Save mchayapol/25c6e6073f484274765099732e76e973 to your computer and use it in GitHub Desktop.
Save mchayapol/25c6e6073f484274765099732e76e973 to your computer and use it in GitHub Desktop.
ngFileUpload sample
'use strict';
angular.module('car2App')
.controller('FileUploadCtrl', ['$scope', '$resource', 'Upload', '$timeout', function ($scope, $resource, Upload, $timeout) {
$scope.cropper = {};
$scope.cropper.sourceImage = null;
$scope.cropper.croppedImage = null;
$scope.bounds = {};
$scope.bounds.left = 0;
$scope.bounds.right = 0;
$scope.bounds.top = 0;
$scope.bounds.bottom = 0;
// multipart upload
$scope.upload = function (dataUrl, name) {
Upload.upload({
url: '/api/images',
data: {
file: Upload.dataUrltoBlob(dataUrl, name)
},
}).then(function (response) {
$timeout(function () {
console.log(response);
$scope.result = response.data;
});
}, function (response) {
if (response.status > 0) {
console.log(response);
$scope.errorMsg = response.status + ': ' + JSON.stringify(response.data);
}
}, function (evt) {
$scope.progress = parseInt(100.0 * evt.loaded / evt.total);
});
};
// for multiple files:
$scope.uploadFiles = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
//Upload.upload({..., data: {file: files[i]}, ...})...;
}
// or send them all together for HTML5 browsers:
//Upload.upload({..., data: {file: files}, ...})...;
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment