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 787 forks source link

[IOS] PDF downloaded with size 0 (empty file) #451

Open marceliano13 opened 5 years ago

marceliano13 commented 5 years ago

Hello I'm trying to download a pdf file and show it on IOS, but the problem is that the file is empty.

Code:

`const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + filename;

const token = await Authentication.getAccessToken();

try { const res = await RNFetchBlob.fetch('GET', uri, { Authorization: 'Bearer ' + token, }); if (res.data) { RNFetchBlob.fs.writeFile(path, res.data, 'base64'); return path; } } catch (ex) { Alert.alert(ex); console.error(ex); }; return null; };`

Path : /Users/amabuilds/Library/Developer/CoreSimulator/Devices/CBA49FC8-5F42-4752-B6C4-C3D30A148BF7/data/Containers/Data/Application/489D75B4-03F8-42DC-90A1-307089E0DA30/Documents/15d8dc77-4195-4ec9-8835-dc2a124cceeb.pdf

Uri is an http:// ....

Thank You

Update After some debugging i found that if i change the extension from .pdf to .txt it works ... but i really need .pdf

Thank You

marceliano13 commented 5 years ago

I found the solution. We must use WriteStream instead of WriteFile. Thank You

rajeshde commented 4 years ago

Can you see the downloaded the PDF file in your phone's files app? I can download the PDF successfully from an url, but can not found it in files app.

Ajmal0197 commented 3 years ago

Issue solved by doing this:

const actualDownload = () => {
    const { dirs } = RNFetchBlob.fs;
    const dirToSave = Platform.OS == 'ios' ? dirs.DocumentDir : dirs.DownloadDir
    const configfb = {
        fileCache: true,
        useDownloadManager: true,
        notification: true,
        mediaScannable: true,
        title: pdfInfo.pdf,
        path: `${dirToSave}/${pdfInfo.pdf}`,
    }
    const configOptions = Platform.select({
        ios: {
            fileCache: configfb.fileCache,
            title: configfb.title,
            path: configfb.path,
            appendExt: 'pdf',
        },
        android: configfb,
    });

    console.log('The file saved to 23233', configfb, dirs);

    RNFetchBlob.config(configOptions)
        .fetch('GET', `https://aquatherm.s3.ap-south-1.amazonaws.com/pdfs/${pdfInfo.pdf}`, {})
        .then((res) => {
            if (Platform.OS === "ios") {
                RNFetchBlob.fs.writeFile(configfb.path, res.data, 'base64');
                RNFetchBlob.ios.previewDocument(configfb.path);
            }
            setisdownloaded(false)
            if (Platform.OS == 'android') {
                showSnackbar('File downloaded');
            }
            console.log('The file saved to ', res);
        })
        .catch((e) => {
            setisdownloaded(true)
            showSnackbar(e.message);
            console.log('The file saved to ERROR', e.message)
        });
}
JPGrefaldo commented 4 months ago

I found the solution. We must use WriteStream instead of WriteFile. Thank You

Thanks @marceliano13, In my case I'm downloading a base64 pdf data from Server.