Getting : TypeError: undefined is not a function (evaluating 'filePath.replace('file://', '')') while uploading a blob to firebase storage

Getting : TypeError: undefined is not a function (evaluating 'filePath.replace('file://', '')') while uploading a blob to firebase storage

我正在开发一个带有 firebase 存储的小型 React Native POC,用户可以在其中选择图像并将其上传到 /uid/ 目录下的 firebase 存储。 我正在使用 react-native-firebase 在应用程序中使用 firebase。

我有图像的 uri,我使用 RNFetchBlob 创建它的 blob,然后尝试将 blob 上传到 firebase。

这是处理 blob 制作和上传的代码。

handleUploadTask = (mime = 'application/octet-stream') => {
    const {imageSource} = this.state;
    this.setState({uploading: true});

    const Blob = RNFetchBlob.polyfill.Blob;
    const fs = RNFetchBlob.fs;

    fs.readFile(imageSource.uri, 'base64')
        .then((data) => {
            return Blob.build(data, { type: `${mime};BASE64` })
        }).then(blob => {
            console.log('going to upload');
            this.uploadBlobToFirebase(blob);
        }).catch(error => {
            console.error('fs error:', error);
        })
}

这里是 uploadBlobToFirebase

uploadBlobToFirebase = (blob) => {
    const currentUserId = firebase.auth().currentUser.uid;
    const path = `/${currentUserId}/`;
    const unsubscribe = firebase.storage().ref(path).putFile(blob).on(
        firebase.storage.TaskEvent.STATE_CHANGED,
        (snapshot) => {
        const  progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        //restricting progress to be shown with only 0,2,4,... 98, 100.
        if (progress % 2 == 0) {
            this.setState({
                uploadProgress: progress
            })
        }

        if (snapshot.state === firebase.storage.TaskState.SUCCESS) {
            this.setState({
                uploading: false,
            }, () => {
                //TODO: should also add this image to flatlist.
                //Get download url and set to image in flatlist.
                Alert.alert('Upload completed');
            })

          }
        },
        (error) => {
            this.setState({
                uploading: false,
            }, ()=>{
                unsubscribe();
                console.error(error);
            })
        },
      ); 
}

函数 handleUploadTask 在上传按钮的 onPress 上被调用并且运行良好直到 console.log('going to upload') 然后结束并出现错误:

'fs error:', { [TypeError: undefined is not a function (evaluating 'filePath.replace('file://', '')')] line: 115903, column: 41, sourceURL: 'http://10.0.2.2:8081/index.delta?platform=android&dev=true&minify=false' }

我这里一片空白。无法找到导致问题的 file.replace 及其原因。 任何帮助,将不胜感激。谢谢。

putFile 需要位于本机存储上的文件的字符串路径。

您需要将 blob 保存到设备临时存储中的一个文件,然后将该临时文件的路径提供给 putFile 方法。

React Native Firebase 确实提供了一些有助于定位设备上某些目录的静态信息,例如临时目录位于 firebase.storage.Native.TEMPORARY_DIRECTORY_PATH。有关这些文档,请参阅 here

它的设计是这样的,因为通过 React Native 桥传输 files/blobs 与保持一切原生相比,性能会受到很大影响。

根据您的示例代码,您应该能够执行以下操作;

firebase.storage().ref(path).putFile(imageSource.uri).on(/* ... */);

包含 putFile 示例的参考文档位于此处:https://rnfirebase.io/docs/v5.x.x/storage/reference/Reference#putFile