简体   繁体   English

如何从 React.js 中的 firebase 获取文件

[英]How can I fetch file from firebase in React.js

I have uploading function to firebase with React.js, I use put method for upload images to firebase, now I need to download that picture from firebase, could you help me please? I have uploading function to firebase with React.js, I use put method for upload images to firebase, now I need to download that picture from firebase, could you help me please? my cod for uploading file is:我上传文件的代码是:

import React, { useState, useEffect } from 'react'
import { storage } from '../../firebase/firebase'

const Upload = () => {

    const [photo, setPhoto] = useState(null)
    const [url, setUrl] = useState(null)


    const imageChangeHandler = (event) => {
        setPhoto(event.target.files[0])
    }

    const fileUploadHandler = () => {

        const uploadTask = storage.ref(`image/${photo.name}`).put(photo);
        uploadTask.on(
            'state-changed',
            snapshot => { },
            error => {
                console.log(error);
            },
            () => {
                storage.ref('image').child(photo.name).getDownloadURL()
                    .then(url => {
                        setUrl(url)
                    })
            }
        )
    }

    return (
        <div className="upload-box">
            <div className="form-upload">
                <input onChange={imageChangeHandler} type="file" className="upload-input" />
                <button onClick={fileUploadHandler} type="submit" value="Send" className="upload-btn">Upload</button>
                <ul>
                    <li>
                        <img width="200px" height="200px" src={url} alt="firebase_uladed_image" />
                    </li>
                </ul>
            </div>
        </div>
    )
    }

     export default Upload

you need to use useEffect to get image from firebase something like this你需要使用 useEffect 从 firebase 像这样

useEffect(()=>{
   fetchApi
   setImage() 
}, [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 React.js 从 firebase 获取嵌套文档 - How to fetch nested document from firebase using React.js 如何在 React.js 初始化时获得 Firebase 身份验证? - How can I get Firebase authentication on initialization with React.js? 通过 react.js 从 firebase 获取反应原生登录的方法 - Method to fetch react native sign In from firebase by react.js 如何在react.js中将三个firebase collections中的数据提取到一张表中 - How to fetch data from three firebase collections into one table in react.js 如何在react.js文件中编写php脚本? - How can I write php scripts in react.js file? 如何在 React js 中从 firebase 存储下载文件? - How can I download a file from firebase storage in react js? 从 React.js 中的 json 文件中获取数据 - Fetch Data from json file in React.js 如何在 React.js 应用程序中显示来自 json 文件的图像? - How can I display images in React.js app from a json file? React.JS = Promise / Fetch API具有重复代码,我如何捆绑到其自己的函数中 - React.JS = Promise/Fetch API has repetitive code, how can I bundle into its own function 如何使用 React.js 中的 useParams 从组件中获取数据? - How can I fetch data out of a component, using useParams in React.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM