wkh237 / react-native-fetch-blob

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

[Android] Downloading an image fails when directory doesn't exist but works on iOS #215

Open dwilt opened 7 years ago

dwilt commented 7 years ago

We are downloading some images in our application. It's working beautifully on iOS. Here is our fetch:

const documentsDir = RNFetchBlob.fs.dirs.DocumentDir;
export const storyAssetsPath = `${documentsDir}/storyAssets`;

const downloadImage = async url => {
    const id = uuid();
    const filename = `${id}.jpg`;
    const path = `${storyAssetsPath}/${filename}`;

    await RNFetchBlob.config({
        fileCache: true,
        path
    }).fetch('GET', url);

    return filename;
};

Note the storyAssets folder that we're downloading the image into.

On Android, if the folder does not exist, it will not download the images correctly. It will seemingly succeed as the res comes back from the .fetch successfully but the images aren't actually there. If I manually create the folder on the device:

adb shell
root@vbox86p:/ $run-as com.greatjonesstreet
root@vbox86p:/ $ cd /data/data/com.greatjonesstreet
root@vbox86p:/data/data/com.greatjonesstreet $ mkdir storyAssets

the images download correctly. Any ideas?

react-native: 0.37.0 react-native-fetch-blob: 0.10.0

wkh237 commented 7 years ago

@dwilt , thanks for reporting this issue. Looks like on Android it does not create folder automatically, as an alternative, you can use fs.exists and fs.mkdir to check and create the folder before download started.

dwilt commented 7 years ago

Yep, that's what I did. Should probably do that anyway - it's more explicit.

colorfulberry commented 7 years ago

@dwilt , I have suffered same things. Have you find a solution about this ?

dwilt commented 7 years ago

@colorfulberry I found a workaround, like @wkh237 suggested too. Here's what I'm doing:

const assetsDirExists = await RNFetchBlob.fs.isDir(storyAssetsPath);

if (!assetsDirExists) {
    await RNFetchBlob.fs.mkdir(storyAssetsPath);
}

I first check to see if that path exists. if it doesn't, I create it.

colorfulberry commented 7 years ago

@dwilt Thanks. 🎄