我尝试将图片上传到Firebase。我正在使用'react-native-image-picker',但每次我都会收到错误。 Firebase存储:字符串与格式'base64'不匹配:找到无效字符。我看到许多帖子与此问题相关,但没有一个正在发挥作用。
uploadImage = response = >
{
let metadata = {
contentType: response.type
}
let randomId = shortid.generate()
let fileExtension
if (Platform.OS == "android") fileExtension = response.type.split("/")[1]
else
{
fileExtension = response.fileName.split(".")[1]
}
let storagePath =
"/Base_Url/" +
this.props.username +
"/" +
this.props.sessionId +
"/" +
randomId +
"." +
fileExtension
let storingRef = firebase.storage()
.ref(storagePath);
storingRef.putString(response.data, "base64url", metadata)
.on(
firebase.storage.TaskEvent.STATE_CHANGED,
snapshot = >
{
var progress =
snapshot.bytesTransferred / snapshot.totalBytes * 100;
if (progress === 100)
{
let downloadURL = FireBaseUtils.getDownloadURL(storagePath);
this.setState(
{
url: snapshot.downloadURL
});
}
else
{
switch (snapshot.state)
{
case firebase.storage.TaskState.PAUSED:
break;
case firebase.storage.TaskState.RUNNING:
break;
}
}
},
error = >
{
switch (error.code)
{
case "storage/unauthorized":
break;
case "storage/canceled":
break;
case "storage/unknown":
break;
}
}
);
};
答案 0 :(得分:0)
尝试使用putString(message, 'base64')
(不是base64url
)
如果Blob,File或Uint8Array不可用,您可以使用 putString()方法上传raw,base64,base64url或data_url 编码字符串到云存储。
https://firebase.google.com/docs/storage/web/upload-files
希望有所帮助