2016-08-24 359 views
3

需要写入文件(pdf)并将其存储在设备内存中。我正在使用react-native-fs库。React本地写入PDF文件使用React-Native-f(IOS)

我想使用户能够使用iBooks读取此文件。

这些反应天然-FS的常量

  • MainBundlePath(字符串)的绝对路径主束 目录
  • CachesDirectoryPath(字符串)的绝对路径缓存 目录
  • DocumentDirectoryPath(字符串)文档的绝对路径 目录

  • Tem poraryDirectoryPath(string)的绝对路径临时 目录(仅适用于iOS)

这是我的下载代码

RNFS.downloadFile({ 
     fromUrl: fileLink,   
     toFile: RNFS.MainBundlePath + '/' + fileName 
    }).promise.then((dwResult) => { 

     console.log(dwResult.jobId + 'jobid'); 
     return true 
    }).catch((err) => { 

     alert(err); 
     return false 
    }) 
    } 

我有什么分配TOFILE,以便将写入文件可以使用可见iBooks的。 注意:我可以使用DocumentDirectoryPath编写文件应用程序的路径。

回答

1

在iOS上有一些限制,因此您可能无法直接使用iBooks打开文件。然而,react-native-fetch-blob的最新beta版本有一个API,它显示一个选项菜单并让用户决定如何处理文件。

或者,你可以直接预览与默认文档查看器的文件中,也显示一个Share Button选项菜单

下载一个例子,打开一个PDF文件react-native-fetch-blob

RNFetchBlob.config({ 
    fileCache : true, 
    appendExt : 'pdf' 
    }) 
    .fetch('GET',`${TEST_SERVER_URL}/public/book.pdf`) 
    .then((res) => { 
    // open the document directly 
    RNFetchBlob.ios.previewDocument(res.path()) 
    // or show options 
    // RNFetchBlob.ios.openDocument(res.path()) 
    }) 
+0

你的意思是如果我编写或下载一个fil e使用我的应用程序,我无法使用ibooks或其他东西预览它。我想在我的应用程序关闭时预览它们。这不可能? –