首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文档/下载中保存文件-本机

在文档/下载中保存文件-本机
EN

Stack Overflow用户
提问于 2022-02-23 00:11:13
回答 2查看 3.4K关注 0票数 5

是否可以使用react本机文档或下载保存文件?我正在尝试保存一个pdf文件,并使用react本机-fs:

代码语言:javascript
复制
let file = await RNHTMLtoPDF.convert(options)
const destinationPath = RNFS.DocumentDirectoryPath + "/" + fileName + ".pdf"
RNFS.copyFile(file.filePath, destinationPath)

但是它保存在根目录(/data/user/0/com.prodhealth.mobile.Dev/files/TestTest_2022-02-22_2022-02-22.pdf)中,普通用户无法访问它。

有没有办法直接将其保存在手机内部存储的文档/下载中?我需要那个用户可以很容易地访问这个文件。

EN

回答 2

Stack Overflow用户

发布于 2022-02-23 09:38:57

您可以使用反应-本机-取-布

  • cp(src:string,dest:string):承诺

dirs这个常量是一个包含常用文件夹的散列映射:

  • DocumentDir
  • MainBundleDir (只能用于访问嵌入在iOS应用程序上的文件)
  • DownloadDir (仅限安卓)

示例

代码语言:javascript
复制
RNFetchBlob.fs.cp(SRC_PATH, DEST_PATH)
.then(() => { ... })
.catch(() => { ... })
票数 2
EN

Stack Overflow用户

发布于 2022-11-23 15:02:10

可以,停那儿吧。对我来说很管用。

代码语言:javascript
复制
        let file = await RNHTMLtoPDF.convert(options);
        const destinationPath = RNFS.DownloadDirectoryPath;
        const FileName = 'filename.pdf';

        const destinationFile = destinationPath + "/" + FileName;
        
        RNFS.copyFile(file.filePath, destinationFile)
            .then(result => {
                
                // Delete a file on the project path using RNFS.unlink
                return RNFS.unlink(file.filePath)
                    .then(() => {
                        console.log('FILE DELETED');
                    })
                    // `unlink` will throw an error, if the item to unlink does not exist
                    .catch((err) => {
                        console.log(err.message);
                    });
            })
            .catch(err => {
                console.log('err', err);
            }); 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71229881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档