Created
November 6, 2016 21:06
-
-
Save schroffl/3712896ab1ff72ec1ab257110f0bad6e to your computer and use it in GitHub Desktop.
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
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