Created
February 26, 2017 10:46
-
-
Save machariamarigi/26e354b6d6d1b1ac3729fd4a4dbfbb74 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
const config = { | |
apiKey: "AIzaSyAvH19ZdgOODfZagnI1f7f49TjLgV90JYs", | |
authDomain: "fir-storage-d4a72.firebaseapp.com", | |
databaseURL: "https://fir-storage-d4a72.firebaseio.com", | |
storageBucket: "fir-storage-d4a72.appspot.com", | |
messagingSenderId: "237836003148" | |
}; | |
firebase.initializeApp(config); | |
// Get reference to firebase storage service | |
const storage = firebase.storage(); | |
// Create a storage reference from our storage service | |
const storageRef = storage.ref(); | |
// Database refrence | |
const database = firebase.database() | |
const imagesRef = database.ref('images'); | |
function handleFileSelect(evt) { | |
evt.stopPropagation(); | |
evt.preventDefault(); | |
let file = evt.target.files[0]; | |
let metaData = { | |
'contentType': file.type | |
} | |
// Push to CHILD path | |
storageRef.child('images/' + file.name).put(file, metaData).then((snapshot) => { | |
console.log('Uploaded', snapshot.totalBytes, 'bytes'); | |
console.log(snapshot.metadata); | |
// Set url of image | |
let url = snapshot.downloadURL; | |
console.log('File Available At', url); | |
imagesRef.push({imageUrl: url }); | |
document.getElementById('linkbox').innerHTML = '<a href="' + url + '">Click For File</a>'; | |
}).catch((error) => { | |
console.error('Upload Failed: ', error); | |
}); | |
} | |
window.onload = function() { | |
document.getElementById('file').addEventListener('change', handleFileSelect, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment