Skip to content

Instantly share code, notes, and snippets.

@schroffl
Created November 6, 2016 21:06
Show Gist options
  • Save schroffl/3712896ab1ff72ec1ab257110f0bad6e to your computer and use it in GitHub Desktop.
Save schroffl/3712896ab1ff72ec1ab257110f0bad6e to your computer and use it in GitHub Desktop.
function handleFiles(files) {
var file = files[0];
if(!file)
fileList.innerHTML = '<p>No files selected</p>';
else {
var form = document.forms.namedItem('uploadForm');
var formData = new FormData();
formData.set('fileElem', file);
getAlbumCover(file, function(cover) {
formData.set('imageElem', cover);
// Now your formData is populated and can be sent to the server
});
}
}
function getAlbumCover(data, callback) {
// musicmetadata is an asynchronous function, therefore you pass a function to it that gets called as soon as the meta data is fetched
// Hence 'getAlbumCover' has to be an asynchronous function as well.
musicmetadata(data, function(err, result) {
if(err)
console.log(err);
var picture = result.picture[0];
if(picture) {
var albumBlob = new Blob([picture.data], {'type':'image/' + picture.format});
// Run the callback and pass the albumBlob as a parameter
callback(albumBlob);
} else
imageList.innerHTML = '<p>No image available in Meta Data<p>';
});
// You can't return the albumBlob because it's not available yet (and its not defined in this scope either)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment