DEV Community

cozeniths
cozeniths

Posted on

2 1 1 1 1

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

The only thing worse than downtime? No Answers.

If you’re sometimes frustrated with opaque infrastructure, sluggish support, and mysterious outages, we prepared a webinar just for you

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️