Skip to content

Instantly share code, notes, and snippets.

@tahoeRobbo
Last active February 2, 2016 02:27
Show Gist options
  • Save tahoeRobbo/a61a5427c472c501f0c2 to your computer and use it in GitHub Desktop.
Save tahoeRobbo/a61a5427c472c501f0c2 to your computer and use it in GitHub Desktop.
Example Uploading an Local Image to Firebase using FileReader
/*CORRECT WORKING FUNCTION TO CONVERT TO BASE 64 AND DISPLAY PREVIEW*/
//var readFile;
var previewFile = function() {
var preview = document.querySelector('#preview');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.onloadend = function() {
preview.src = reader.result
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = '';
}
} // previewFile()
var uploadFile = function() {
console.log('uploadFile Clicked')
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
var inputCaption = document.querySelector('#photoCaption').value;
/* reader.onloadend = function() {
reader.result;
}*/
//RUNS AFTER THE READER.READAS...(FILE) IN THE IF STATEMENT. DELIVERS RESULTS AFTER EVERYTHING IS LOADED
//SIMILAR TO PROMISE RESOLUTION
reader.onloadend = function() {
var imgRef = new Firebase('https://YOUR_APP.firebaseio.com/photos');
imgRef.push({
data : reader.result,
caption : inputCaption,
});
document.querySelector('#photoCaption').value = '';
}
//MAKES SURE THERE IS A FILE SELECTED -- IF THERE IS, READ THE FILE AS A DATAURL -- CAN ACCESS RETURN
//VIA READER.RESULT
if (file) {
console.log("within if statement fired");
reader.readAsDataURL(file);
}
else {
console.log('else fired uploadFile');
}
}; // uploadFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment