rn-fetch-blob icon indicating copy to clipboard operation
rn-fetch-blob copied to clipboard

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

Open marceliano13 opened this issue 6 years ago • 4 comments

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 avatar Oct 01 '19 11:10 marceliano13

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

marceliano13 avatar Oct 01 '19 14:10 marceliano13

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.

rajeshde avatar Dec 25 '19 08:12 rajeshde

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)
        });
}

Ajmal0197 avatar Jan 15 '21 15:01 Ajmal0197

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.

JPGrefaldo avatar Jul 01 '24 11:07 JPGrefaldo