Last active
April 13, 2021 03:58
-
-
Save joeljerushan/0ccfa4cae5d691df47f5a52c53b4a6c3 to your computer and use it in GitHub Desktop.
Firebase Image Upload with ImageUpload React
This file contains hidden or 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
putFeaturedImagetoStorage(key){ | |
// the return value will be a Promise | |
let set = this | |
let time = Date.now(); | |
let name = '/FOLDER_NAME/' + 'featured-' + time | |
return firebase.storage().ref().child(name).put(set.state.featured_image[0]).then(function(snapshot) { | |
snapshot.ref.getDownloadURL().then(function(downloadURL) { | |
let imagePath = { | |
featured_image_download_url: downloadURL, | |
featured_image_name: name, | |
featured_image_folder: folder, | |
featured_image_file_name: file_name, | |
} | |
//save imagePath path to firebase firestore document or anywhere | |
}); | |
}).catch((error) => { | |
console.log('One failed: ', error.message) | |
}); | |
} | |
//function to set image | |
onDropPoster(picture){ | |
this.setState({ | |
featured_image: picture, | |
}) | |
} | |
//render method | |
<ImageUploader | |
singleImage={true} | |
withIcon={true} | |
buttonText='Choose images' | |
onChange={this.onDropPoster} | |
imgExtension={['.jpg']} | |
maxFileSize={5242880} | |
withPreview={true} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment