使用react native在服务器上下载一个文件



我已经尝试在服务器上下载一个文件几天了,但我看到的示例来自不同的url,示例是"example.com.br/file.extension">

在我的例子中,url是->F:Inetpubvhosts {url} {url} Clients {idClient}DefaultIMG.jpg

数据来自api

getSingleDocument: async (documentoId) => {
try {
const auth = await AsyncStorage.getItem('auth');
const req = await fetch(`${api}/${guid}/documentospadrao/ObterPeloId/?documentoId=${documentoId}&clienteId=${auth}`, {
method: 'Get',
headers: {
Accept: 'application/json',
'Content-type': 'application/json',
}
})
const json = req.json();
return json;
} catch (err) {
console.log('API::getSingleDocument: ' + err)
}
},

我解决了

我在Listdocuments.js组件中做了这个

这个handleOpenFile是TouchableOpacity=onPress(handleOpenFile)并有一个菜单,像反应原生纸做这个动作

<Menu
visible={visible}
onDismiss={closeMenu}
anchor={
<TouchableOpacity onPress={openMenu}>
<Icon
name="ellipsis-v"
color="#505050"
size={18}
style={{ marginRight: '5%' }}
/>
</TouchableOpacity>
}>
<Menu.Item onPress={handleOpenFile} title="Open file" />
</Menu>

const handleOpenFile = () => { 
const Path = data.PathDoc;
const PathReplace = Path.replace("F:\Inetpub\vhosts\example.com.br\", "https://");

const localFile = `${RNFS.DocumentDirectoryPath}/${data.Title}.${data.Extension}`;
const options = {
fromUrl: PathReplace,
toFile: localFile
};
RNFS.downloadFile(options).promise
.then(() => FileViewer.open(localFile))
.then(() => {
})
.catch(error => {
alert("Error.")
});
closeMenu();
}

我使用了lib - react-native-fs和react-native-fileviewer

相关内容

最新更新