简体   繁体   English

无法在 Firebase 存储上上传图像

[英]Not able to upload image on Firebase Storage

I am trying to upload Image using Firebase in Firebase Storage, but file doesn't uploads completely.我正在尝试在 Firebase 存储中使用 Firebase 上传图像,但文件没有完全上传。 It shows the size of image 9 bytes only, and when downloaded, it can't be previewed.它仅显示图像 9 字节的大小,下载时无法预览。

Here is the code i am using:-这是我正在使用的代码:-

const [image, setImage] = useState(null)
    const [uploading, setUploading] = useState(false);
    const [transferred, setTransferred] = useState(0);

  const uploadImage = async () => {
    if( image == null ) {
      return null;
    }
    
    const uploadUri = image;
    let filename = uploadUri.substring(uploadUri.lastIndexOf('/') + 1);
    console.log(filename)
    // Add timestamp to File Name
    const extension = filename.split('.').pop(); 
    const name = filename.split('.').slice(0, -1).join('.');
    filename = name + Date.now() + '.' + extension;
    console.log("filename")
    console.log(filename)

    setTransferred(0);

    const storageRef = firebase.storage().ref(`photos/${filename}`);
    console.log("storageRef")
    const task = storageRef.put(uploadUri);
    console.log("storageRef")
    console.log(storageRef)
    // Set transferred state
    task.on('state_changed', (taskSnapshot) => {
      console.log(
        `${taskSnapshot.bytesTransferred} transferred out of ${taskSnapshot.totalBytes}`,
      );

      setTransferred(
        Math.round(taskSnapshot.bytesTransferred / taskSnapshot.totalBytes) *
          100,
      );
      console.log(transferred)
    });

    try {
      await task;

      const url = await storageRef.getDownloadURL();

      setUploading(false);
      setImage(null);

      alert(
        'Image uploaded!',
        'Your image has been uploaded to the Firebase Cloud Storage Successfully!',
      );
      return url;

    } catch (e) {
      console.log(e);
      return null;
    }

  };


  const takephotofrommlib = () => {
    ImagePicker.openPicker({
      width: 300,
      height: 300,
      cropping: true,
    }).then((image) => {
      console.log(image);
      const imageUri = Platform.OS === 'ios' ? image.path : image.path;
      setImage(image.path);
      console.log("image.path")
      console.log(image.path)

    });
  };

在此处输入图像描述

I am using react-native-image-crop-picker.我正在使用 react-native-image-crop-picker。 I am using Firebase but not react-native firebase.我正在使用 Firebase 但不是 react-native firebase。 Please Help!请帮忙!

i just make a file uploadFile.js here's a code我只是做一个文件uploadFile.js这是一个代码

import storage from "@react-native-firebase/storage";

export default async function uploadFile(ref,fileName,file) {   
  if(!file) return
      const tarea=file
  if (tarea&& tarea.indexOf("http://") == 0 || tarea&&tarea.indexOf("https://") == 0) 
{
    // do something here
    return tarea
  }
  const reference = storage().ref(`${ref}/${fileName}`);
   await reference.putFile(file).catch(err=>{console.log("error upload",err);
   })
  return await storage()
  .ref(`${ref}/${fileName}`)
  .getDownloadURL().catch(err=>{console.log("download eror",err);
  });
}

you can use like this img= await uploadFile('photos',"fileName",filePath)你可以像这样使用img= await uploadFile('photos',"fileName",filePath)

In img you can get the download urlimg你可以下载 url

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM