簡體   English   中英

React Native Expo 如何使用 expo 圖像選擇器將圖像上傳到 Firebase

[英]React Native Expo How to upload an image to Firebase using expo Image picker

我正在嘗試將圖像上傳到我的 Firebase,但我無法弄清楚。

這是我的代碼

_pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.All,
        allowsEditing: true,
        aspect: [4, 3],
        quality: 1
    });

    if (!result.cancelled) {
        this.uploadImage(result.uri, 'test-image')
            .then(() => {
                console.log('it work')
            })
            .catch(error => {
                console.log('it does not work')
                console.error(error)
            })
    }
};

uploadImage = async (uri, imageName) => {
    const response = await fetch(uri);
    const blob = response.blob();

    const ref = firebase.storage().ref().child(`images/${imageName}`);
    return ref.put(blob);
}

我收到這個錯誤。

FirebaseStorageError {
 "code_": "storage/invalid-argument",
 "message_": "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.",
 "name_": "FirebaseError",
 "serverResponse_": null,
}

使用此代碼:

`const blob = await response.blob();`

代替:

const blob = response.blob();

解決您的問題。

嘗試var ref= firebase.storage().ref().child('images/'+imageName)而不是你的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM