Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active August 29, 2015 13:56
Show Gist options
  • Save Deraen/9007883 to your computer and use it in GitHub Desktop.
Save Deraen/9007883 to your computer and use it in GitHub Desktop.
Angular function for fileupload
fun.uploadImage = function($files, $event, status) {
var def = $q.defer();
if (!$files) return def.promise;
status.error = null;
status.active = true;
status.perc = 0;
var req;
var urlUpload = _.isString($files);
var linkDragDrop = $files.length === 0;
if (urlUpload || linkDragDrop) {
// "Upload" by url
var url = $files;
if (linkDragDrop) {
console.log($event);
var link = $event.dataTransfer.getData('text/uri-list');
console.log(link);
if (link.length > 0) {
url = angular.element(link).src;
console.log(url);
}
}
req = $http.post('/api/images', {
url: url,
});
} else {
req = $upload.upload({
url: '/api/images',
file: $files[0],
}).progress(function(evt) {
status.perc = parseInt(100.0 * evt.loaded / evt.total);
});
}
req.success(function(data) {
def.resolve(data);
}).error(function(err) {
status.error = err;
def.reject();
}).finally(function() {
status.active = false;
});
return def.promise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment