0

I'm not sure what to call the files, but the context is that I have a bunch of data that I would like to ship with my app. I need to find a way to store this data so that I can load it into the database. (Or better yet, just ship it with a prefilled db)

Here are the solutions that I've seen:

Storing the data in code as a json blob I can't do this because I have quite a lot of data, a few MB or so and I would like to be able to compress it.

Load a file from the public folder I think this is a create-react-app specific API and I did not use that. But if there's a way to get a public or static folder to read arbitrary files from, that'll be great.

Read a file using react-native-fs I'm not sure where to put the file in my application so that I can access it. This seems to give me an empty folder to write files to. I don't know where in my app directory I can put the files if I want it to be read by this.

I would also like to have these files compressed, and only loaded when I need them to be. I think using a require('path/to/file.json') loads the file every time I use the app.

How would I go about reading a file from my app?

I'm coding for android if that matters and the database I'm using is watermelondb.

CJX3711
  • 83
  • 6
  • Are you able to get the uri for the file that you want to upload? – Rohit Aggarwal Jun 21 '21 at 13:09
  • I'm not trying to upload a file, just to read it. The problem is I don't know where to put the file in my project so that I can access it. – CJX3711 Jun 22 '21 at 01:58
  • If you want to just show the PDF, I'd prefer not to download it and instead show it using some libraries like [react-native-pdf](https://reactnative.dev/docs/text#onpress) to view. – Rohit Aggarwal Jun 22 '21 at 13:49
  • This is not a PDF. I'm trying to store a compressed file that I can load into my database on first run. – CJX3711 Jun 23 '21 at 21:07

1 Answers1

0

You need to use react-native-asset to put files to your project and access them with require keyword.

Example for file with path ./assets/files/some-file.mp3:

  1. Add or modify react-native.config.js
module.exports = {
 project: {
   ios: {},
   android: {},
 },
 dependencies: {
 },
 assets: [
   './assets/files'
 ],
};
  1. Run npx react-native-asset
  2. Use the file:
const file = require('./assets/files/some-file.mp3')
Alexander Danilov
  • 3,038
  • 1
  • 30
  • 35