1

I use rn-fetch-blob for download file.

I don't have any problems, when I use URL, like this const FILE_URL = "https//:google.com/my-file.pdf". File downloaded. Everything is OK.

But if I save my file in android/app/main/src/assets folder, and change my FILE_URL to const FILE_URL = RNFetchBlob.fs.asset(my-file.pdf) i receive error Can only download HTTP/HTTPS URIs How to resolve this error?

Code:

const TestScreen = () => {
  const fileUrl = RNFetchBlob.fs.asset('my-file.pdf');

  const checkPermission = async () => {
    // Function to check the platform
    // If Platform is Android then check for permissions.

    if (Platform.OS === 'ios') {
      downloadFile();
    } else {
      try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
          {
            title: 'Storage Permission Required',
            message:
              'Application needs access to your storage to download File',
          },
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          // Start downloading
          downloadFile();
          console.log('Storage Permission Granted.');
        } else {
          // If permission denied then show alert
          Alert.alert('Error', 'Storage Permission Not Granted');
        }
      } catch (err) {
        // To handle permission related exception
        console.log('++++' + err);
      }
    }
  };

  const downloadFile = () => {
    // Get today's date to add the time suffix in filename
    setLoading(true);
    let FILE_URL = fileUrl;
    // Function to get extention of the file url
    let file_ext = getFileExtention(FILE_URL);

    file_ext = '.' + file_ext[0];

    // config: To get response by passing the downloading related options
    // fs: Root directory path to download
    const {config, fs} = RNFetchBlob;
    let pathToFile = fs.dirs.DownloadDir + `/my-file.pdf`;
    let options = {
      addAndroidDownloads: {
        fileCache: true,
        path: pathToFile,
        description: 'downloading file...',
        notification: true,
        // useDownloadManager works with Android only
        useDownloadManager: true,
      },
    };
    config(options)
      .fetch('GET', FILE_URL, 60000)
      .then(res => {
        // Alert after successful downloading
        console.log('res -> ', JSON.stringify(res));
      });
  };

  const getFileExtention = fileUrl => {
    // To get the file extension
    return /[.]/.exec(fileUrl) ? /[^.]+$/.exec(fileUrl) : undefined;
  };
  return <Button title="Donload" onPress={checkPermission} />;
};
  • Can you test if the file at the path exists by running: RNFetchBlob.fs.exists(RNFetchBlob.fs.asset('my-file.pdf'))? Are you sure the file exists? Also, what is the output of RNFetchBlob.fs.asset('my-file.pdf')? What is the path, does it have "bundle-assets://" at the start? – Aifos Si Prahs Oct 19 '22 at 15:11
  • Can you share more code to get full context? For example, add the code of the full function where you use the RNFetchBlob.fs.asset function? – Aifos Si Prahs Oct 19 '22 at 15:13
  • 1
    If I use RNFetchBlob.fs.exists(RNFetchBlob.fs.asset('my-file.pdf')) i get {"_h": 0, "_i": 0, "_j": null, "_k": null} If I use RNFetchBlob.fs.asset('my-file.pdf') - i get bundle-assets://my-file.pdf – Ivan Ivanov Oct 19 '22 at 15:25
  • I'll have a look in a few minutes – Aifos Si Prahs Oct 19 '22 at 15:28
  • Can you run `RNFetchBlob(RNFetchBlob.fs.asset('my-file.pdf')) .then((exist) => { console.log(exist ? 'exists' : 'doesn't exist') }) .catch((err) => { console.log(err) });` ? – Aifos Si Prahs Oct 20 '22 at 12:30
  • Error - object is not a function https://prnt.sc/812y9LajdOum – Ivan Ivanov Oct 20 '22 at 13:47
  • Can you share your full code, for example create a CodeSandbox? – Aifos Si Prahs Oct 20 '22 at 15:58
  • What will be if you try to do `RNFetchBlob.fs.exists(RNFetchBlob.fs.asset('my-file.pdf')) .then((exist) => { console.log(exist ? 'exists' : 'doesn't exist') }) .catch((err) => { console.log(err) });`? – Aifos Si Prahs Oct 20 '22 at 16:00
  • 1
    When I try RNFetchBlob.fs.exists(RNFetchBlob.fs.asset('my-file.pdf')) .then((exist) => { console.log(exist ? 'exists' : 'doesn't exist') }) .catch((err) => { console.log(err) }); I get error object is not a function prnt.sc/812y9LajdOum – Ivan Ivanov Oct 20 '22 at 16:30
  • If you have used RNFetchBlob for downloading locale file from assets folder, maybe you have some code examples? Because everywhere I found only examples with uri, but not with locale file. – Ivan Ivanov Oct 20 '22 at 16:32
  • On the https://prnt.sc/812y9LajdOum screenshot you're running RNFetchBlob(..., not RNFetchBlob.fs.exists(.. like I mentioned in my last message. – Aifos Si Prahs Oct 20 '22 at 16:58
  • I'll try to create a snippet, but it would just be better to do it directly with your codebase – Aifos Si Prahs Oct 20 '22 at 16:58
  • Will let you know a bit later – Aifos Si Prahs Oct 20 '22 at 16:58
  • 1
    I got "doesn't exist". But i have file in assets folder - https://prnt.sc/rih_-CjOoPox – Ivan Ivanov Oct 20 '22 at 17:53
  • Can you try to use a relative path like './images/my-file.pdf' or try to use an absolute fs like `resolve = require( 'path' ).resolve; resolve('../../bb/tmp.txt')` – Aifos Si Prahs Oct 20 '22 at 19:26
  • 1
    Like this const fileUrl = './android/app/main/src/assets/my-file.pdf' or const fileUrl = require( './android/app/main/src/assets/my-file.pdf) – Ivan Ivanov Oct 21 '22 at 11:42
  • Yes, depending on where the file is located. You can try that. Let me know if it succeeds. – Aifos Si Prahs Oct 21 '22 at 13:09
  • If my fileURL = '../../android/app/src/main/assets/my-file.pdf', I got error- https://prnt.sc/xEyip8V819yd If I use require('../../android/app/src/main/assets/my-file.pdf'), I also get error - ++++TypeError: Cannot convert undefined value to object – Ivan Ivanov Oct 21 '22 at 13:25
  • const resolve = require( 'path' ).resolve; /*path is a node module*/ const fileUrl = resolve('./images/my-file.pdf'); /*path relative to the file where you call this line of code*/; Try this – Aifos Si Prahs Oct 21 '22 at 13:38
  • Or better try this: const resolve = require( 'path' ).resolve; /*path is a node module*/ const fileUrl = RNFetchBlob.fs.asset(resolve('./images/my-file.pdf')); /*path relative to the file where you call this line of code*/; – Aifos Si Prahs Oct 21 '22 at 13:46
  • const fileUrl = RNFetchBlob.fs.asset( resolve('../../android/app/src/main/assets/my-file.pdf'), ); Error - TypeError: undefined is not a function. I have installed path node module! – Ivan Ivanov Oct 21 '22 at 15:37
  • Have you imported it in your file so that resolve function is available (`const resolve = require( 'path' ).resolve;`)? Again, do you have an NDA or can create a codesandbox with your code/or even better create a public GitHub repo, it'll be way better so that I can not just guide you, but come to a solution myself and send you the working one. – Aifos Si Prahs Oct 21 '22 at 15:57
  • 1
    I pushed it to github https://github.com/romanmaha/TestScreen – Ivan Ivanov Oct 21 '22 at 16:48
  • Thank you, great, I'll let you know when I come to a solution. – Aifos Si Prahs Oct 21 '22 at 17:01
  • Perhaps the problem is that fetch only works with files on the server, not local paths? config(options) .fetch('GET', FILE_URL, 60000) .then(res => { // Alert after successful downloading console.log('res -> ', JSON.stringify(res)); }); – Ivan Ivanov Oct 23 '22 at 10:16
  • Yes, but it also should work with Base64. I thought of reading the file from path natively and downloading it with rn-fetch-blob – Aifos Si Prahs Oct 23 '22 at 14:39
  • It works with remote URLs only by default – Aifos Si Prahs Oct 23 '22 at 14:59
  • Perhaps you know how to download local file? – Ivan Ivanov Oct 24 '22 at 07:50
  • RNFetchBlob is only working for url based on HTTP or HTTPS not local uri – AbdulmateenChitrali Jul 13 '23 at 15:19

0 Answers0