简体   繁体   English

使用reart-native-fs 在React-Native 中保存文件

[英]Saving a files in React-Native using reart-native-fs

After saving a file using react-native-fs where does that file go?使用react-native-fs保存文件后,该文件去哪里了?

It's not explained in the documentation or I couldn't find and it's hard to figure.文档中没有解释,或者我找不到并且很难弄清楚。

When you create a file you have to specify a directory where to store the file.创建文件时,必须指定存储文件的目录。 In the example below the path variable shows where the file will be written to.在下面的示例中, path变量显示了文件将写入的位置。

var RNFS = require('react-native-fs');

// create a path you want to write to
// :warning: on iOS, you cannot write into `RNFS.MainBundlePath`,
// but `RNFS.DocumentDirectoryPath` exists on both platforms and is writable
var path = RNFS.DocumentDirectoryPath + '/test.txt';

// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
  .then((success) => {
    console.log('FILE WRITTEN!');
  })
  .catch((err) => {
    console.log(err.message);
  });

There are several places you can store your files您可以在多个位置存储文件

The following directories are available:以下目录可用:

  • MainBundlePath (String) The absolute path to the main bundle directory (not available on Android) MainBundlePath (String) 主包目录的绝对路径(在 Android 上不可用)
  • CachesDirectoryPath (String) The absolute path to the caches directory CachesDirectoryPath (String) 缓存目录的绝对路径
  • ExternalCachesDirectoryPath (String) The absolute path to the external caches directory (android only) ExternalCachesDirectoryPath (String) 外部缓存目录的绝对路径(仅限安卓)
  • DocumentDirectoryPath (String) The absolute path to the document directory DocumentDirectoryPath (String) 文档目录的绝对路径
  • TemporaryDirectoryPath (String) The absolute path to the temporary directory (falls back to Caching-Directory on Android) TemporaryDirectoryPath (String) 临时目录的绝对路径(在 Android 上回退到 Caching-Directory)
  • LibraryDirectoryPath (String) The absolute path to the NSLibraryDirectory (iOS only) LibraryDirectoryPath (String) NSLibraryDirectory 的绝对路径(仅限 iOS)
  • ExternalDirectoryPath (String) The absolute path to the external files, shared directory (android only) ExternalDirectoryPath (String) 外部文件的绝对路径,共享目录(仅限安卓)
  • ExternalStorageDirectoryPath (String) The absolute path to the external storage, shared directory (android only) ExternalStorageDirectoryPath (String) 外部存储的绝对路径,共享目录(仅限安卓)

So all you have to do is choose the directory.所以你所要做的就是选择目录。 Note these directories are on the device.请注意,这些目录位于设备上。

Seeing the files on the device查看设备上的文件

iOS Simulator iOS模拟器

If you want to find the file on your simulator on iOS all you need to do is console.log the path, which should look something like this如果你想在你的 iOS 模拟器上找到这个文件,你需要做的就是 console.log 路径,它应该看起来像这样

/Users/work/Library/Developer/CoreSimulator/Devices/2F8BEC88-BF42-4D6B-81D4-A77E6ED8CB00/data/Containers/Data/Application/BC16D2A6-55A2-475C-8173-B650A4E40CF1/Documents/

Open Finder , select Go to folder打开Finder ,选择Go to folder

在此处输入图片说明

And then paste in the path and that will open the folder where the file is.然后粘贴路径,这将打开文件所在的文件夹。

在此处输入图片说明

Android Emulator安卓模拟器

If you console.log the path for the file you should get something like this如果你 console.log 文件的路径,你应该得到这样的东西

/data/user/0/com.awesomeapp/files/

You have to run the app from Android Studio, then you need to access the Device View Explorer .您必须从 Android Studio 运行该应用程序,然后您需要访问Device View Explorer You do that by going to View -> Tool Windows -> Device File Explorer你可以通过View -> Tool Windows -> Device File Explorer

在此处输入图片说明

That should then open a windows that looks like this, where you can then browse to the file path that you were given above.然后应该会打开一个看起来像这样的窗口,然后您可以在其中浏览到上面给出的文件路径。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM