[英]Saving a files in React-Native using reart-native-fs
使用react-native-fs
保存文件后,該文件去哪里了?
文檔中沒有解釋,或者我找不到並且很難弄清楚。
創建文件時,必須指定存儲文件的目錄。 在下面的示例中, 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);
});
您可以在多個位置存儲文件
以下目錄可用:
MainBundlePath
(String) 主包目錄的絕對路徑(在 Android 上不可用)CachesDirectoryPath
(String) 緩存目錄的絕對路徑ExternalCachesDirectoryPath
(String) 外部緩存目錄的絕對路徑(僅限安卓)DocumentDirectoryPath
(String) 文檔目錄的絕對路徑TemporaryDirectoryPath
(String) 臨時目錄的絕對路徑(在 Android 上回退到 Caching-Directory)LibraryDirectoryPath
(String) NSLibraryDirectory 的絕對路徑(僅限 iOS)ExternalDirectoryPath
(String) 外部文件的絕對路徑,共享目錄(僅限安卓)ExternalStorageDirectoryPath
(String) 外部存儲的絕對路徑,共享目錄(僅限安卓)
所以你所要做的就是選擇目錄。 請注意,這些目錄位於設備上。
如果你想在你的 iOS 模擬器上找到這個文件,你需要做的就是 console.log 路徑,它應該看起來像這樣
/Users/work/Library/Developer/CoreSimulator/Devices/2F8BEC88-BF42-4D6B-81D4-A77E6ED8CB00/data/Containers/Data/Application/BC16D2A6-55A2-475C-8173-B650A4E40CF1/Documents/
打開Finder
,選擇Go to folder
然后粘貼路徑,這將打開文件所在的文件夾。
如果你 console.log 文件的路徑,你應該得到這樣的東西
/data/user/0/com.awesomeapp/files/
您必須從 Android Studio 運行該應用程序,然后您需要訪問Device View Explorer
。 你可以通過View
-> Tool Windows
-> Device File Explorer
然后應該會打開一個看起來像這樣的窗口,然后您可以在其中瀏覽到上面給出的文件路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.