javascript - React Native Expo 如何使用 expo 图像选择器将图像上传到 Firebase

标签 javascript react-native expo firebase-storage react-native-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();

解决您的问题。

关于javascript - React Native Expo 如何使用 expo 图像选择器将图像上传到 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595314/

相关文章:

javascript - 使用原型(prototype)从对象内部调用对象上的函数

javascript - React Native ListView 到 FlatList 迁移

iphone - 使用 Firebase JS SDK 对 Expo/React Native 应用程序进行应用程序检查

react-native - React Native 应用程序无法在 Expo 应用程序中启动

javascript - 如何使用 jquery sortable 对一组元素进行排序并防止放置一个点

javascript - Fullcalendar 禁用拖放但启用可编辑

javascript - 在子组件中传递和调用的函数无权访问父状态值

animation - 使用新的动画 api 重复动画

javascript - 无法从字符串访问 this.props 到函数

javascript - 客户端只接收一些服务器发送的事件