简体   繁体   English

在 React.js 中使用 Dropzone 时如何将图像上传到 Firebase 存储并获取图像的下载 URL

[英]How to Upload Images to Firebase Storage and Get Image's Download URL's When Using Dropzone in React.js

I have a <Dropzone/> component in my react website that allows my users to upload multiple images just by either clicking or dragging and dropping.我的反应网站中有一个<Dropzone/>组件,它允许我的用户通过单击或拖放来上传多个图像。

When I upload the images they are being saved in my firebase storage but the URL's for each indiviual image are not being set and i get a firebase error that reads:当我上传图像时,它们被保存在我的 firebase 存储中,但没有设置每个单独图像的 URL,我收到 firebase 错误,内容如下:

"Uncaught (in promise) FirebaseError: Firebase Storage: Object 'propertyImages/Picture1.png+4b80d-6dd0-4b40-cadc-8e7845677bbd' does not exist. (storage/object-not-found)
{
  "error": {
    "code": 404,
    "message": "Not Found."
  }
}"

I don't know why it isn't setting the url for each image, i believe it is because it is overwritting the previous download urls.我不知道为什么它没有为每个图像设置 url,我相信这是因为它覆盖了以前的下载 url。 How can i make Dropzone work properly?如何使 Dropzone 正常工作?

What am I doing wrong?我究竟做错了什么?

Here is my code:这是我的代码:

  import Dropzone, {useDropzone} from "react-dropzone"

  const [urls,setUrls] = useState([]);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDropAccepted: onFilesDrop, maxFiles: 5 });




  function onFilesDrop(files) {
    files.map((item, index) => {
        let uploadTask = storage.ref(`propertyImages/${item.name}+${uuid()}`).put(item);
        uploadTask.snapshot.ref.getDownloadURL().then(function (downloadURL) {
            setUrls(prevState => {
                return [...prevState, downloadURL]
            })
        })
    })
}
return (
  <CardTitle className="mb-3 h4">Property Images (MAX IMAGES 5)</CardTitle>
                    <div className="dropzone" >
                    <Dropzone
                        onDrop={acceptedFiles =>
                          onFilesDrop(acceptedFiles)
                        }
                      >
                        {({ getRootProps, getInputProps }) => (
                          <div>
                            <div
                              className="dz-message needsclick"
                              {...getRootProps()}
                            >
                              <input {...getInputProps()} />
                              <div className="dz-message needsclick">
                                <div className="mb-3">
                                  <i className="display-4 text-muted bx bxs-cloud-upload" />
                                </div>
                                <h4>Drop files here or click to upload.</h4>
                              </div>
                            </div>
                          </div>
                        )}
                      </Dropzone>
                      
)

update onFilesDrop function更新文件删除 function

function onFilesDrop(files) {
    files.map((item, index) => {
        let uploadTask = storage.ref(`propertyImages/${item.name}+${uuid()}`).put(item);
        console.log(uploadTask);
        uploadTask.on(
          "state_changed",
          snapshot => {
          },
          error => {
            console.log(error);
          },
          async () => {
            await storage
            .ref("images")
            .child(item.name)
            .getDownloadURL()
            .then(urls => {
              console.log(urls) //log url if you want
              setUrls((P) => [...P, urls]);
            })
          }
        )
      
    })

暂无
暂无

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

相关问题 如何将多个图像上传到 firebase v9 存储并获取所有下载 URL 以使用 Vue.js 在云 Firestore 数据库中更新它 - How to upload multiple images to firebase v9 storage and get all download URL 's to update it in cloud firestore database using Vue.js 当我上传图像 firebase 存储 getdownloadUrl() 将下载 url 放入 Firestore Collections 但未在 React JS 中设置 - When I upload a image firebase storage getdownloadUrl() put download url In Firestore Collections but not set it in React JS 成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL - Firebase get Download URL after successful image upload to firebase storage 如何从具有 url React js 的 firebase 存储中获取图像 - How to get image from firebase storage having the url React js 我正在尝试将图像上传到 firebase 存储并获取下载 url - I'm trying to upload an image to firebase storage and get the download url 如何从Firebase存储中获取react.js的异步图像数据? - How get async image data for react.js from firebase storage? Firebase Storage 和 Dropzone.js 多张图片上传按钮按下 - Firebase Storage and Dropzone.js multiple image upload on button press 如何将图像上传到 firebase 存储,然后返回下载 url? - How to upload an image to firebase storage and then return the download url? 如何对firebase存储图像URL进行软编码 - How to softcode firebase storage image url's 上传多张图片到Firebase存储得到url张 - Get url of multi images when upload them to Firebase Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM