获取存储在 React Native 文档目录中的文件名数组 [英] Get the array of filenames stored in document directory in React Native

查看:94
本文介绍了获取存储在 React Native 文档目录中的文件名数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

案例

我已将音频文件下载到名为/tracks/的文件夹下的文档目录中:

RNFetchBlob.fs.dirs.DocumentDir + '/tracks/'

毫无疑问,我可以按名称阅读每个音频:

RNFetchBlob.fs.dirs.DocumentDir + '/tracks/' + 'audio1.mp3'

问题:我想获得所有音频的列表.我可以在 File Access API 中看到,我们可以读取文件,但我无法找到如何从文件夹/tracks/"中获取音频文件列表.

我只想在那个目录中有一个文件名数组.

文件访问 API 链接:

我想我已经接近了,但输出似乎难以解析.:(

最后:(这是怎么做的)

 var TRACK_FOLDER = RNFetchBlob.fs.dirs.DocumentDir + '/tracks/';console.log('TRACK_FOLDER 中的文件列表 = ', RNFetchBlob.fs.ls(TRACK_FOLDER));RNFetchBlob.fs.ls(TRACK_FOLDER).then((文件)=>{控制台.日志(文件.长度);控制台日志(文件);控制台日志(文件[0]);})

输出:

希望这对那里的人有所帮助.

解决方案

RNFetchBlob.fs.ls 返回一个 promise.

所以你可以使用 .then/.catch 访问它

RNFetchBlob.fs.ls(TRACK_FOLDER).then(files => {控制台日志(文件);}).catch(error => console.log(error))

或者你可以使用 async/await

尝试{让文件 = 等待 RNFetchBlob.fs.ls(TRACK_FOLDER);控制台日志(文件);} 捕捉(错误){控制台日志(错误);}

你可以阅读更多关于 RNFetchBlob.fs.ls 此处.另请注意,RNFetchBlob 的存储库已移至此处 https://github.com/joltup/rn-fetch-blob

CASE

I have downloaded audio files to my document directory under the folder named /tracks/ as :

RNFetchBlob.fs.dirs.DocumentDir + '/tracks/'

No doubt I can read each audio by their name as :

RNFetchBlob.fs.dirs.DocumentDir + '/tracks/' + 'audio1.mp3'

QUESTION: I want to get the list of all the audios. I can see in File Access API , we can read file but I am unable to find how to get the list of audio files from the folder '/tracks/'.

I just want to have an array of filenames in that directory.

File Access API link : https://github.com/wkh237/react-native-fetch-blob/wiki/File-System-Access-API#dirs

P.S: I didn't want to go for other file access plugins. I don't know if I have to search other libraries for file listing .

UPDATE

With the following code:

var TRACK_FOLDER = RNFetchBlob.fs.dirs.DocumentDir + '/tracks/';

console.log('Files LIST in Tracks Folder = ', RNFetchBlob.fs.ls(TRACK_FOLDER));

OUTPUT IS :

I think I'm getting close but's output seems difficult to parse. :(

FINALLY:(this is the way how it is done)

    var TRACK_FOLDER = RNFetchBlob.fs.dirs.DocumentDir + '/tracks/';

    console.log('Files list in TRACK_FOLDER = ', RNFetchBlob.fs.ls(TRACK_FOLDER));

     RNFetchBlob.fs.ls(TRACK_FOLDER)
    .then( (files) =>{ 
        console.log(files.length);  
        console.log(files); 
        console.log(files[0]); 

    })

OUTPUT:

Hope this helps somebody out there.

解决方案

RNFetchBlob.fs.ls returns a promise.

So you can either access it with using .then/.catch

RNFetchBlob.fs.ls(TRACK_FOLDER).then(files => {
  console.log(files);
}).catch(error => console.log(error))

or you can use async/await

try {
  let files = await RNFetchBlob.fs.ls(TRACK_FOLDER);
  console.log(files);
} catch (error) {
  console.log(error);
}

You can read more about RNFetchBlob.fs.ls here. Also note that the repository for RNFetchBlob has moved to here https://github.com/joltup/rn-fetch-blob

这篇关于获取存储在 React Native 文档目录中的文件名数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆