So I'm allowing user's upload certain images in the storage of firebase, and each file is named after their user id. So when a user uploads an image, I get a url that for that file
And so when a user changes the uploaded image, I'd like to delete the previously updated image from the storage. I've seen how they do it in the docs, but that requires the file name which I don't have access to.
This is the code when a user uploads an image
function uploadImg(e) { const file = e.target.files[0]; if (file && file.size < 30000000) { var storageRef = firebase .storage() .ref(`${user.uid}/images`) .child(file.name); const task = storageRef.put(file); task.on( "state_changes", function progress(snap) { setLoading(true); const percentage = (snap.bytesTransferred / snap.totalBytes) * 100; loadingref.current.style.height = percentage + "%"; }, function error() { setNotifibool(true); setNotifi({ text: "Try Again!", icon: "fal fa-exclamation-circle" }); }, function complete() { setLoading(false); storageRef.getDownloadURL().then((url) => { setState(url); }); setNotifi({ text: "Image Uploaded!", icon: "fal fa-check-circle" }); } ); } else { window.alert("too big"); } } https://stackoverflow.com/questions/67052164/deleting-files-from-firebase-storage April 12, 2021 at 11:06AM
没有评论:
发表评论