I’m developing a React Native app using Expo, and I’m trying to download a file (Excel format) from an API and save it to the device’s internal storage so that it can be accessed and opened by the user. Currently, I’m able to download the file successfully, but it’s getting saved to a temporary cache directory (FileSystem.documentDirectory). Here’s the relevant code snippet I’m using:
const handleDownload = async () => {
const fileUri = `${FileSystem.documentDirectory}report.xlsx`;
const downloadResumable = FileSystem.createDownloadResumable(
apiUrl,
fileUri,
{
headers: {
Authorization: `Bearer ${token}`,
},
},
callback
);
try {
const res = await downloadResumable.downloadAsync();
if (res && res.uri) {
console.log('Finished downloading to ', res.uri);
const downloadsDir = `${FileSystem.documentDirectory}Download/`;
await FileSystem.makeDirectoryAsync(downloadsDir, { intermediates: true });
const newUri = downloadsDir + 'report.xlsx';
await FileSystem.moveAsync({
from: res.uri,
to: newUri,
});
console.log('File saved to:', newUri);
alert('File has been saved to Downloads folder');
} else {
alert('Download failed or file is undefined');
}
} catch (e) {
console.error('Error downloading or saving file:', e);
alert('Error downloading or saving file');
}
};
After downloading the file, it’s currently getting saved to a cache directory (FileSystem.documentDirectory), but I need to save it to the device’s internal storage so that it’s accessible and can be opened by the user. How can I modify my code to achieve this in React Native using Expo?
note: my response from BE is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet