我正在使用react-native-fs从服务器下载文件并保存它们。下载后,我将使用 react-native-file-viewer 打开它们。这个过程完全正常:我可以在查看器中下载并打开它,但我无法从查看器中将文件保存在文件应用程序中,尽管我可以保存在 iCloud 中但不能保存在 iPhone 中。 下载的文件存储在类似
的地方/var/mobile/Containers/Data/Application/CCF6CD16-A62A-48BB-92F3-3021195CFE0C/Documents/react-native-pdf.pdf
但这不会显示在"file"应用中。
我用来下载和查看文件的代码如下
RNFS.downloadFile({
fromUrl: 'http://www.mywebsite.com/react-native-pdfurl.pdf',
toFile: `${RNFS.DocumentDirectoryPath}/react-native-pdfurl.pdf`,
}).promise.then((r) => {
console.log('yo yo yo ');
this.setState({ isDone: true });
const path = `${RNFS.DocumentDirectoryPath}/react-native-pdfurl.pdf`;
FileViewer.open(path)
.then(() => {
// success
})
.catch(error => {
// error
});
RNFS.readDir(RNFS.DocumentDirectoryPath).then(files => {
console.log(files);
})
.catch(err => {
console.log(err.message, err.code);
});
});
readDir 获取保存文件的名称路径。但这并没有反射(reflect)在"file"应用程序中的任何文件夹中。 我的问题是如何以文件应用程序中显示的方式保存文件。
最佳答案
以下代码下载文件并打开它。如果文件已经存在,它将直接打开它。
downloadOpenClick = async (item) => {
try {
let platformName = 'ios';
if (Platform.OS === 'ios'){
platformName = 'ios';
}else{
platformName = 'android';
}
const selectedFile = item;
var dirType=null;
if(Platform.OS === 'ios'){
dirType = RNFS.DocumentDirectoryPath;
}else{
await this.requestStoragePermission();
dirType = RNFS.ExternalStorageDirectoryPath+'/AppName';
}
RNFS.mkdir(dirType+`/Folder`).then(files => {
RNFS.mkdir(dirType+`/Folder/SubFolder`).then(files => {
//console.log(files);
}).catch(err => {
//console.log(err.message, err.code);
});
}).catch(err => {
//console.log(err.message, err.code);
});
var exists = false;
RNFS.exists(`${dirType}/Folder/SubFolder/${selectedFile}`).then( (output) => {
if (output) {
exists = true;
const path = `${dirType}/Folder/SubFolder/${selectedFile}`;
FileViewer.open(path)
.then(() => {
// success
})
.catch(error => {
// error
console.log('error');
console.log(error);
});
} else {
const selectedFileUrl = selectedFile.replace(/\s/g, '%20');
RNFS.downloadFile({
fromUrl: `https://mywebsite/api/getAttachment?selectedFile=${selectedFileUrl}`,
toFile: `${dirType}/Folder/SubFolder/${selectedFile}`,
background: true,
begin: (res) => {
console.log(res);
this.setState({ contentLength: res.contentLength});
},
progress: (res) => {
this.setState({ showSpinner: true });
var prog = res.bytesWritten/res.contentLength
this.setState({ downloaded : prog});
console.log(this.state.downloaded);
}
}).promise.then((r) => {
//console.log(r);
this.setState({ showSpinner: false });
this.setState({ downloaded : 0});
const path = `${dirType}/${tipoDesc}/${oggetto}/${selectedFile}`;
FileViewer.open(path)
.then(() => {
// success
})
.catch(error => {
// error
console.log('error');
console.log(error);
});
}).catch(error => {
console.log('error');
console.log(error);
});;
}
});
} catch (error) {
console.log('error');
console.log(error);
}
};
使用
react-native-file-viewer and react-native-fs
关于ios - 如何在 iOS 中使用 React Native 保存下载的文件并在文件应用程序中查看?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55146640/