Angular function for fileupload
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
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