🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

react-native-du

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-du

A file download utility for react native

0.3.2
latest
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created

react-native-du

A file download utility for react native

Installation

npm install react-native-du

or

yarn add react-native-du

Usage

import * as React from 'react';

import { StyleSheet, View, Button } from 'react-native';
import {
  downloadFile,
  downloadStatus,
  DOWNLOAD_MANAGER_STATUS,
} from 'react-native-du';

export default function App() {
  const downloadPdf = async () => {
    try {
      const id = await downloadFile({
        url: 'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg',
        fileName: 'cat.jpg',
        desc: 'This is a cat ',
      });
      console.log({ id });
      const status = await downloadStatus(id?.[0] || '');
      const idx: string = status?.[0] || '';
      console.log({ status: DOWNLOAD_MANAGER_STATUS[idx] });
    } catch (err) {
      console.log(err);
    }
  };

  return (
    <View style={styles.container}>
      <Button title="Download" onPress={downloadPdf} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  box: {
    width: 60,
    height: 60,
    marginVertical: 20,
  },
});

Functions

async function downloadFile({
  url,
  fileName,
  desc = '',
  onPermissionDenied,
  onError,
})
// params
{
  url : 'https://cdn.pixabay.com/photo/2017/11/09/21/41/cat-2934720_1280.jpg' // request url,
  fileName: 'cat.jpg' // file Name with extension
  desc: 'Description of download manager' // optional
  onPermissionDenied: ()=>{}  // optional , this function gets called if storgae permission is denied
  onError: (error)=>{} // optional , this function gets called if any exception is thrown
}

// returns
['downloadId'] or undefined


async function downloadStatus(downloadId: string)
// params
{
  downloadId : 'id' // download id returned by downloadFile function,
}

// returns
['download_manager_status'] or undefined



Permissions

You might have to add following permission in AndroidManifest.xml file


  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

Made with create-react-native-library

FAQs

Package last updated on 11 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts