1

I need to download files from the server and get the link of these files that have been downloaded to device in purpose to update the state:

const [ fileLink, setFileLink ] = useState('');

const result = ... // get the link to file somehow
setFileLink(result);

I tried the following solutions:

  • React Native Fetch Blob rn-fetch-blob. These is an error occurs "Null is not an object (evaluating 'RNFetchBlob.DocumentDir')". As far as i understood, rn-fetch-blob doesn't work with Expo.
  • Expo Filesystem expo-file-system. It doesn't provide to possibility to fetch files with GET method. Even if i use the link 'http://.../file.jpg' it have been downloaded to local folder FileSystem.documentDirectory, but doesn't give a link to the file, so a cannot update the state.

Is there any solution to download files from the server and get the link to these files?

Yossi
  • 5,577
  • 7
  • 41
  • 76

1 Answers1

2

MY EXP ON THIS

I was working on a file sharing app, and also needed to download and upload files to server.

Upload worked just fine, but download wasn't a piece of cake. After a lot of search this is what I discovered.

RESOURCES YOU HAVE

Since you are using Expo, you only got one resource that's expo's own FileSystem library.

Why RN-Fetch-Blob won't work?

Android's FileSystem is a native module so it requires linking so that's why other libraries like rn-fetch-blob or react-native-fs won't work on expo, expo does not support any library that requires linking!

WHAT ARE THE SOLUTIONS?

SOLUTION1

So with Expo, Your only option is to use Expo's built in filesystem library, FileSystem library does support to download from server (Remote URL) .

with this function FileSystem.downloadAsync(uri, fileUri, options) you can download files from your server!

You can read More about this function on Documentation: https://docs.expo.io/versions/latest/sdk/filesystem/#filesystemdownloadasyncuri-fileuri-options

KEEP IN MIND: Please note that these files are only available to be used inside your App, you'll of course get the link/uri to the local files (in cached directory) but users can only access these files via your App because these are saved into your App's cache/doc directory and that's hidden for user to be accessed via file manager or any other Apps! But it will do the work for you.

SOLUTION2

You can Eject your App from Expo and Simply use react-native-fs or rn-fetch-blob. You can read more about ejecting expo Here

Community
  • 1
  • 1
Kashan Haider
  • 1,036
  • 1
  • 13
  • 23
  • thanks for information! But how to get a link for a file which have been downloaded? ```FileSystem.downloadAsync(uri, fileUri, options)``` can return only uri of folder, not a file –  Jan 11 '20 at 21:46
  • Since the file is in the cache and cannot be accessed anywhere except your App so there's no URL/LINK for the file, because it can only be accessed via your APP Lets take example of an Image, You download the image you get the URI and you use the URI to display that image inside your APP, You cannot get link to that Image file and you cant access that image outside of your App its in your App's cache. – Kashan Haider Jan 11 '20 at 21:56
  • That is currently not possible in EXPO and it was one of the reason why i left expo. react-native-fs is one of the best fileSystem library if you ever think of leaving expo you can use this library to achieve your goal and its very easy to use and will work perfectly for the purpose you want it for. Thanks!. – Kashan Haider Jan 11 '20 at 21:58
  • Is there a possibility to save a cached file into library, for example via ```expo-media-library``` ? Thanks for information! –  Jan 12 '20 at 10:31
  • 1
    Yes, You get the URI now you can use camera roll to save it to camera roll, i have not tested it but it should work... `import { CameraRoll } from 'react-native'; CameraRoll.saveToCameraRoll( fileURI, 'photo');` – Kashan Haider Jan 12 '20 at 11:34
  • More over, there are many users who have requested internal storage feature on EXPO's forum you can read more on ==> https://expo.canny.io/feature-requests/p/ability-to-save-files-on-internal-storage Please upvote my answer if it helps. thank you – Kashan Haider Jan 12 '20 at 11:35
  • To save with CameraRoll it should be path with extention .jpg or .png, but Filesystem can return only folder path, not file path –  Jan 12 '20 at 12:04
  • I've already read all these information, nothings works of this. Anyway thanks for your help! –  Jan 12 '20 at 12:05
  • I know that nothing works as you want it, but what can we do, when we are using EXPO we have limited resources . so there is no other possible solutions at this time, i told you all the possible ways. It is a requested feature in EXPO from almost a year and the team doesnt show any interest to work on this feature. – Kashan Haider Jan 12 '20 at 12:42
  • 1
    EXPO is very limited and only recommended to use for learning purpose, as soon as you get enough expertise you must get onto react-native-cli it has no limits and almost all type of native libraries are available. :) Have a great day. – Kashan Haider Jan 12 '20 at 12:44
  • Just as a comment on the above. Expo is not limited. Expo can install Native code, etc. – moto Apr 29 '23 at 03:29