Rn-fetch-blob: Does rn-fetch-blob work with Expo?

Created on 11 Jan 2020  路  3Comments  路  Source: joltup/rn-fetch-blob

Hi, i try to use rn-fetch-blob with Expo and there is an error occurs: "null is not an object (evaluating 'RNFetchBlob.DocumentDir')".
I saw a couple of comments that rn-fetch-blob doesn't support with expo. I didn't find any solution, also any information, if it should work with Expo. I try to find any solution to download files and open it.

Sorry if my question is duplicate and thanks for any help.

Most helpful comment

If you're using expo, you can use its four libraries for downloading and sharing files

  • expo-sharing
  • expo-media-library
  • expo-permissions
  • expo-file-system
async function downloadFile(url) {
    // Downloading the file
    let fileLocation = FileSystem.documentDirectory + "test.jpg";
    FileSystem.downloadAsync(url, fileLocation)
    .then(({url}) => {

        // Saving the file in a folder name `MyImages`
        const {status} = await Permissions.askAsync(Permissions.CAMERA_ROLL);
        if (status === "granted") {
            const asset = await MediaLibrary.createAssetAsync(url)
            await MediaLibrary.createAlbumAsync("MyImages", asset, false)
        }

        // Sharing the downloded file
        Sharing.shareAsync(fileLocation);
    })
    .catch(error => {
        console.error(error);
    })
}

It'll download the file and open the share modal for sharing.
Rn-fetch-blob is not supported for expo-projects and this is the only way I am able to find for now.

Maybe useful for some cases, not all.

All 3 comments

rn-fetch-blob implements native code, and so it cannot be used in expo based projects. You would need to eject from expo from my understanding

@Traviskn i am not on expo, but i am still getting this error on android

If you're using expo, you can use its four libraries for downloading and sharing files

  • expo-sharing
  • expo-media-library
  • expo-permissions
  • expo-file-system
async function downloadFile(url) {
    // Downloading the file
    let fileLocation = FileSystem.documentDirectory + "test.jpg";
    FileSystem.downloadAsync(url, fileLocation)
    .then(({url}) => {

        // Saving the file in a folder name `MyImages`
        const {status} = await Permissions.askAsync(Permissions.CAMERA_ROLL);
        if (status === "granted") {
            const asset = await MediaLibrary.createAssetAsync(url)
            await MediaLibrary.createAlbumAsync("MyImages", asset, false)
        }

        // Sharing the downloded file
        Sharing.shareAsync(fileLocation);
    })
    .catch(error => {
        console.error(error);
    })
}

It'll download the file and open the share modal for sharing.
Rn-fetch-blob is not supported for expo-projects and this is the only way I am able to find for now.

Maybe useful for some cases, not all.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MJXin picture MJXin  路  5Comments

kyle-ssg picture kyle-ssg  路  5Comments

Jeijie picture Jeijie  路  3Comments

msqar picture msqar  路  4Comments

drewandre picture drewandre  路  4Comments