DEV Community

cozeniths
cozeniths

Posted on

React Native download remote file

  import RNFetchBlob from 'rn-fetch-blob'


// code for component
   const toast = useToast();

 const downloadCSV = async () => {
        if (loading) return;
        setLoading(true)
        const { fs } = RNFetchBlob;
        let downloadsDir = fs.dirs?.DownloadDir; // Directory where downloaded files are saved
        const fileUrl = 'https://filename'; // URL of the CSV file
        const fileName = 'Sample-Data-' + Date.now() + '.csv'; // Name to save the file with
        // Config for the download
        const configOptions = {
            trusty: true,
            session: "test",
            fileCache: true,
            addAndroidDownloads: {
                useDownloadManager: true,
                notification: true,
                path: `${downloadsDir}/${fileName}`,
                // description: 'Downloading CSV file.',
            },
        };
        // Trigger the download
        let res = await RNFetchBlob.config(configOptions).fetch('GET', fileUrl);
        if (res) {
            toast.show({ title: "Download successfully !" });
            setTimeout(() => {
                setLoading(false);
            }, 500)
        }
    };

Enter fullscreen mode Exit fullscreen mode

Cozeniths

Cozeniths is providing web services, app development and seo optimization

favicon cozeniths.com

Top comments (0)