React Native + Firebase存储:base64和blob无法正常工作 [英] React Native + Firebase Storage: base64 and blob not working
问题描述
我正试图将我的图像文件编码为base64或blob,但是它们都不起作用.我也在使用它: https://github.com/react-community /react-native-image-picker 来管理图像选择器.
I'm trying to use to either encode my image file as base64 or blob, but neither of them are working. I'm also using this: https://github.com/react-community/react-native-image-picker to manage the image picker.
尝试1:使用图像选择器的.data
方法,该方法应该返回base64字符串
Try 1: Using Image picker's .data
method, which is supposed to return a base64 string
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:使用> 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
put at index 0: Expected Blob or File.
However, it is returning: Firebase Storage: Invalid argument in
putat index 0: Expected Blob or File.
尝试3:使用> 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
有人知道我在做什么吗?
Does anyone know what I'm doing wrong?
推荐答案
在我最近使用5.5.6版本的那天,我更新到了6.2.0版本并遇到了同样的问题.
Back in the day I used version 5.5.6 recently I updated to version 6.2.0 and ran to the same issue.
const uri = Platform.OS === 'ios' ? response.uri.replace('file://', '') : response.uri
const imageRef = storage().ref('images/').child("blah")
await imageRef.put(uri)
const imageUrl = await imageRef.getDownloadURL()
我刚刚将.put
更改为.putFile
,然后一切又恢复正常
I just changed .put
to .putFile
and everything worked again
这篇关于React Native + Firebase存储:base64和blob无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!