Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 放入存储的图像保存为';八位组流';而不是图像/jpeg(firebase和ReactNative)_Javascript_Firebase_React Native_Firebase Storage_React Native Image Picker - Fatal编程技术网

Javascript 放入存储的图像保存为';八位组流';而不是图像/jpeg(firebase和ReactNative)

Javascript 放入存储的图像保存为';八位组流';而不是图像/jpeg(firebase和ReactNative),javascript,firebase,react-native,firebase-storage,react-native-image-picker,Javascript,Firebase,React Native,Firebase Storage,React Native Image Picker,我正在使用相机(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.f

我正在使用相机(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()
      })
    })
}

结果: 在Firebase(存储)中,图像保存为应用程序/八位字节流,而不是图像/jpeg。 图像未显示,从存储器下载时显示未定义

任何帮助都将不胜感激。

该方法接受
Blob
Uint8Array
ArrayBuffer
。您的“文件”对象似乎不是这些对象中的任何一个

相反,我们需要将文件读入内存(使用
react native fs
——称为RNFS),然后将该数据与所需的元数据一起上载。因为文件被RNFS读取为base64,所以我们将使用它,因为它接受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()
    }
  });
};

我就是这样修复它的:

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)
}

这就是我得到的错误:ReferenceError:找不到变量:fs我试图这样做:fs const fs=require('fs');但事实并非如此working@Byusa很抱歉,这是我的一个输入错误,应该是
rnfs
而不是
fs
但是,它不起作用。它不显示任何错误,也不将任何内容保存到存储器中。因此
saveImage().catch(e=>console.error(e))
什么也不做?
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)
}