rn-fetch-blob
和 react-native-fs
包,在Android上工作得很好,但是对于iOS,它们只能创建文件到内部应用存储器(因此文件未创建在外部存储器中,即不在iOS文件应用程序中)。我有什么选择来将我创建的文件保存到“Files”应用程序中?我知道这是可能的,例如Slack应用程序允许将文件保存到“Files”应用程序中。rn-fetch-blob
和 react-native-fs
包,在Android上工作得很好,但是对于iOS,它们只能创建文件到内部应用存储器(因此文件未创建在外部存储器中,即不在iOS文件应用程序中)。我有什么选择来将我创建的文件保存到“Files”应用程序中?我知道这是可能的,例如Slack应用程序允许将文件保存到“Files”应用程序中。你需要在你的Info.plist
文件中添加UISupportsDocumentBrowser
键并将其设置为true,或同时添加UIFileSharingEnabled
和LSSupportsOpeningDocumentsInPlace
键。
当用户通过文件应用程序从你的应用文档目录打开文档时,他们会直接在原地编辑文档。更改将保存到你的应用文档目录中。
有关更多信息,请参见此文档。
react-native-fs
在本地目录中保存和下载。
var url = path let fileName = path.split("/").pop() // console.log('fileName', fileName) var ext = this.extention(url); ext = "." + ext[0]; const localFile =
${RNFS.LibraryDirectoryPath}/${fileName}
; // console.log('localFile', localFile)
const options = { fromUrl: path, toFile: localFile, fileCache: true }; if (await RNFS.exists(localFile)) { FileViewer.open(localFile, { displayName: this.props.route.params.conversationSubject }); } else { await RNFS.downloadFile(options).promise .then((res) => { // console.log('res', res) FileViewer.open(localFile); }) .then((res) => { // success // console.log("success", res); }) .catch(error => { // error console.log("Attachment open error: ", error); }); }
我已经为react-native-share模块添加了新功能,可以将文件仅存储在Files
应用程序中。它尚未合并,但您可以使用我的分支版本。
https://github.com/gevorg94/react-native-share/tree/share-files-app。
请将"react-native-share": "X.X.X"
替换为"react-native-share": "git+https://github.com/gevorg94/react-native-share.git#share-files-app",
。
您可以在PR https://github.com/react-native-community/react-native-share/pull/681中找到用法。