joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.84k stars 784 forks source link

Does rn-fetch-blob work with Expo? #517

Closed ghost closed 4 years ago

ghost commented 4 years ago

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.

Traviskn commented 4 years ago

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

maneesha-krishna commented 4 years ago

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

actuallyakash commented 4 years ago

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

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.

praveen-jangir commented 3 years ago

https://github.com/edugyangroup/Expo-blob

earlwood commented 3 years ago

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.

This doesn't work anymore in expo SDK 41 or 42, Does somebody knows the new way to download a file from app?

VityaSchel commented 3 years ago

How to get width and height of image from file:// in expo?

thareekanvar commented 2 years ago

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.

This doesn't work anymore in expo SDK 41 or 42, Does somebody knows the new way to download a file from app?

https://github.com/thareekanvar/expo-downloads-manager try this

rahmanharoon commented 1 year ago

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.

I'm facing some issue while downloading pdf

const url = "<link>";
const downloadPath = FileSystem.documentDirectory + `sample.pdf`;
const { uri } = await FileSystem.downloadAsync(url, downloadPath);
const asset = await MediaLibrary.createAssetAsync(uri); // the code breaks here

Here is the error message i'm getting as an warning and file fails to download at that line

Possible Unhandled Promise Rejection (id: 13):
Error: Could not create asset