[英]How to get file name from download link in firebase storage(Javascript, React)?
I've being trying to get files name from Firebase storage.我一直在尝试从 Firebase 存储中获取文件名。 Right now I got a download link and trying to get name from it.
现在我有一个下载链接并试图从中获取名称。
const showPhoto = () => {
var eventDOM = event.target;
var src = eventDOM.getAttribute('src');
setpickImage(src);
console.log(src);
// src = "https://firebasestorage.googleapis.com/v0/b....."
}
I need file name to delete file from storage.我需要文件名才能从存储中删除文件。
If you want to map a download URL back to a file name, use the refFromURL
method .如果你想map一个下载URL回一个文件名,使用
refFromURL
方法。
Also see the documentation on creating a reference from a download URL , which contains this example:另请参阅有关从下载 URL 创建参考的文档,其中包含以下示例:
var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
From that Reference
you can then get other properties like the file's name .然后,您可以从该
Reference
中获取其他属性,例如文件的名称。
let fileName = httpsReference.name;
To get the file name and url or download link of the videos or files from the firebase storage you can use these two functions in javascript and react
要获取文件名和 url 或从 firebase 存储中获取视频或文件的下载链接,您可以在 javascript 中使用这两个函数并进行反应
// function to fetch videos / files from firebase storage const fetchVideos = async() => { const storageRef = firebase.storage().ref("your storage folder name"); const videos = []; await storageRef.listAll().then(async function(result) { result.items.forEach(async function(videoRef) { // getting the name of the file const videoName = videoRef.name; // getting the url of the file -> calling another function for this const videoUrl = await getVideoUrl(videoRef); // creating the object with name and url const videoObj = { videoName, videoUrl, }; console.log("video obj", videoObj); videos.push(videoObj); }); }).catch(function(error) { // Handle any errors return []; }); } // function to get download url const getVideoUrl = (imageRef) => { const videoLink = imageRef.getDownloadURL().then(function(videoUrl) { console.log("videoUrl", videoUrl); return videoUrl; }).catch(function(error) { // Handle any errors return ""; }); return videoLink; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.