最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Images put to storage are saved as 'octet-stream' rather than imagejpeg (firebase and ReactNative)

programmeradmin2浏览0评论

I am using the camera(react-native-image-Picker) to take a pick and save it to storage. Here is how I am doing it.

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://.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"}

Results: In Firebase (storage) The image is being saved as application/octet-stream instead of image/jpeg. The image is not shown, it says undefined when downloaded from storage.

Any help will be so appreciated.

I am using the camera(react-native-image-Picker) to take a pick and save it to storage. Here is how I am doing it.

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://.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"}

Results: In Firebase (storage) The image is being saved as application/octet-stream instead of image/jpeg. The image is not shown, it says undefined when downloaded from storage.

Any help will be so appreciated.

Share Improve this question edited Jan 22, 2021 at 6:09 Byusa asked Jan 22, 2021 at 4:53 ByusaByusa 3,0972 gold badges22 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

This is how I was able to fix it:

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

The Reference#put() method accepts a Blob, Uint8Array or ArrayBuffer. Your "file" object doesn't appear to be any of these.

Instead, we need to read the file into memory (using react-native-fs - referred to as RNFS) and then upload that data along with the required metadata. Because the file is read as base64 by RNFS, we will use Reference#putString instead as it accepts Base64 strings for uploads.

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

Solution: Image reference in uploadBytesResumable() method

const storageRef = ref(storage,`product-images/${image.name}`);
uploadBytesResumable(storageRef,image);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论