我试图将我的图像文件编码为 base64 或 blob,但它们都不起作用。我也在用这个:https://github.com/react-community/react-native-image-picker https://github.com/react-community/react-native-image-picker管理图像选择器。
尝试 1:使用图像选择器.data
方法,应该返回一个 base64 字符串
const blah = uuid.v4();
firebase
.storage()
.ref('images')
.child(`${blah}.jpg`)
.putString(file.data, 'base64', { contentType: 'image/jpeg' })
但这会抛出:Firebase Storage: String does not match format 'base64': Invalid character found
尝试 2:使用 Blobhttps://github.com/wkh237/react-native-fetch-blob https://github.com/wkh237/react-native-fetch-blob
const uri = file.uri;
const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
data = RNFetchBlob.fs.readFile(uploadUri, 'base64')
blob = RNFetchBlob.polyfill.Blob.build(data, { type: 'application/octet-stream;BASE64' })
const uniqueId = uuid.v4();
const firebaseRef = firebase.storage().ref('images').child(`${uniqueId}.jpg`)
return Observable.fromPromise(firebaseRef.put(blob, { contentType: 'application/octet-stream;BASE64' }));
})
然而,它正在返回:Firebase Storage: Invalid argument in
putat index 0: Expected Blob or File.
尝试3:使用base64https://github.com/wkh237/react-native-fetch-blob https://github.com/wkh237/react-native-fetch-blob
const uri = file.uri;
const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri
data = RNFetchBlob.fs.readFile(uploadUri, 'base64')
const uniqueId = uuid.v4();
const firebaseRef = firebase.storage().ref('images').child(`${uniqueId}.jpg`)
const metadata= {
contentType: 'image/jpeg',
};
firebaseRef.putString(data, 'base64', metadata);
但这再次抛出:Firebase Storage: String does not match format 'base64': Invalid character found
有谁知道我做错了什么?