简体   繁体   English

如何从 firebase 存储(Javascript,React)中的下载链接获取文件名?

[英]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.

相关问题 如何在 React js 中从 firebase 存储下载文件? - How can I download a file from firebase storage in react js? 如何允许所有用户从 firebase_Storage 获取下载链接 - how to allow all users to get download link from firebase_Storage 如何从 firebase 存储下载图像,然后使用 JavaScript 重新上传但名称不同 - how to download an image from firebase storage then reupload it but with different name using JavaScript springboot上传文件到firebase存储后如何获取下载url - How to get the download url after uploading a file to firebase storage in springboot 从 Firebase 中获取下载 URL 存储在 Flutter - Get Download URL from Firebase Storage in Flutter 上传文件到firebase存储Flutter后无法获取下载链接 - Cannot get the download link after uploading files to firebase storage Flutter 如何从 Firebase 存储下载整个文件夹? - How to download entire folder from Firebase Storage? 如何从 firebase 存储下载和显示图像 - How to download and display images from firebase storage 从 firebase 存储下载文件与 HTTP 请求任何人都可以下载 - Download file from firebase storage with HTTP request that anyone can download 如何使用 firebase_admin 从 python 中的 firebase 存储中下载文件 - How to download file from firebase storage in python using firebase_admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM