【问题标题】:Images put to storage are saved as 'octet-stream' rather than image/jpeg (firebase and ReactNative)存储的图像保存为“八位字节流”而不是 image/jpeg(firebase 和 ReactNative)
【发布时间】:2021-04-26 14:04:43
【问题描述】:

我正在使用相机(react-native-image-Picker)进行挑选并将其保存到存储中。 这是我的做法。

const saveImage = async () => {
    const id = firebase.firestore().collection('food').doc().id
    const storageRef = firebase.storage().ref()
    const fileRef = storageRef.child(file.fileName) //name of image to store
    await fileRef.put(file) //store image

    firebase.firestore().collection("food").doc(id).update({
      image: firebase.firestore.FieldValue.arrayUnion({
        name: file.fileName,
        url: await fileRef.getDownloadURL()
      })
    })
}

console.log(typeof file);
gives => "object"

console.log(file);
//gives => 
file = {height: 2322, 
uri:"content://com.photodocumentation.imagepickerprovidlib_temp_7a0448df-1fac-4ac7-a47c-402c62ecce4c.jpg", 
width: 4128, 
fileName: "rn_image_picker_lib_temp_7a0448df-1fac-4ac7-a47c-402c62ecce4c.jpg", 
type: "image/jpeg"}

结果: 在 Firebase(存储)中,图像被保存为 application/octet-stream 而不是 image/jpeg。 图片未显示,从存储下载时显示未定义。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript firebase react-native firebase-storage react-native-image-picker


    【解决方案1】:

    这就是我能够修复它的方法:

    const uploadImage = async () => {
        const response = await fetch(file.uri)
        const blob = await response.blob();
        var ref = firebase.storage().ref().child("FolderName");
        return ref.put(blob)
    }
    

    【讨论】:

      【解决方案2】:

      Reference#put() 方法接受 BlobUint8ArrayArrayBuffer。您的“文件”对象似乎不是其中的任何一个。

      相反,我们需要将文件读入内存(使用react-native-fs - 称为 RNFS),然后将该数据连同所需的元数据一起上传。因为文件被 RNFS 读取为 base64,我们将使用 Reference#putString 代替,因为它接受 Base64 字符串进行上传。

      const rnfs = require('react-native-fs');
      
      const saveImage = async () => {
        const capture = /* this is your "file" object, renamed as it's not a `File` object */
        const fileRef = firebase.storage().ref(capture.fileName);
        const captureBase64Data = await rnfs.readFile(capture.uri, 'base64');
        const uploadSnapshot = await fileRef.putString(captureBase64Data, 'base64', {
          contentType: capture.type,
          customMetadata: {
            height: capture.height,
            width: capture.width
          }
        });
      
        // const id = colRef.doc().id and colRef.doc(id).update() can be replaced with just colRef.add() (colRef being a CollectionReference)
      
        return await firebase.firestore().collection('food').add({
          image: {
            name: capture.fileName,
            url: await fileRef.getDownloadURL()
          }
        });
      };
      

      【讨论】:

      • 这是我得到的错误:ReferenceError:找不到变量:fs 我试图这样做:fs const fs = require('fs');但它仍然无法正常工作
      • @Byusa 抱歉,这是我的错字,应该是 rnfs 而不是 fs
      • 还是不行。它没有显示任何错误,也没有将任何内容保存到存储中。
      • 所以saveImage().catch(e => console.error(e)) 什么都不做?
      猜你喜欢
      • 2021-06-18
      • 2020-12-04
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2016-09-27
      • 2019-02-25
      相关资源
      最近更新 更多