存储的图像保存为 'octet-stream' 而不是 image/jpeg (firebase 和 React Native)
Images put to storage are saved as 'octet-stream' rather than image/jpeg (firebase and ReactNative)
我正在使用相机(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。
图片未显示,从存储中下载时显示未定义。
任何帮助将不胜感激。
Reference#put()
方法接受 Blob
、Uint8Array
或 ArrayBuffer
。您的“文件”对象似乎不是这些中的任何一个。
相反,我们需要将文件读入内存(使用 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()
}
});
};
这是我修复它的方法:
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)
}
我正在使用相机(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。 图片未显示,从存储中下载时显示未定义。
任何帮助将不胜感激。
Reference#put()
方法接受 Blob
、Uint8Array
或 ArrayBuffer
。您的“文件”对象似乎不是这些中的任何一个。
相反,我们需要将文件读入内存(使用 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()
}
});
};
这是我修复它的方法:
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)
}