I am trying to download a file in react native but using library (ReactNativeBlobUtil). for android it is working fine but for IOS the file is going in document directory but not on the desired location. it is going under the several folders and there too it is not accessible.
const downloadReport = async () => {
try {
const { dirs } = ReactNativeBlobUtil.fs;
const dirToSave = LAYOUT.isiOS ? dirs.DocumentDir : dirs.LegacyDownloadDir;
const date = new Date();
const fileName = `download${Math.floor(
date.getDate() + date.getSeconds() / 2
)}.pdf`;
const config = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
path: `${dirToSave}/${fileName}`,
description: t("downloading"),
},
addIOSDownloads: {
notification: true,
path: `${dirToSave}/${fileName}`,
description: t("downloading"),
},
};
await ReactNativeBlobUtil.config(config)
.fetch("GET", FILE_URL, {})
.then((res) => {
console.log("The file saved to ", res.path());
});
showSuccessMessage(t("fileDownloadedSuccessfully"));
} catch (error) {
showErrorMessage(t("failedToDownloadFile"));
}
};
this is the file location i am getting in the IOS.
/var/mobile/Containers/Data/Application/54A6714F-9875-4612-A0AB-EDC92FA65C15/Documents/ReactNativeBlobUtil_tmp/ReactNativeBlobUtilTmp_mlp7gzc9srh232rrggpngt
and as i am downloading the pdf file but the file extension is not there. The saved file’s type is data. which is not desired.
This is my info.plist file where i have added the required permissions.
<key>UIFileSharingEnabled</key>
<true/>
<key>CFBundleGetInfoString</key>
<string></string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key> UIFileSharingEnabled</key>
<true/>
<key>NSDownloadsFolderUsageDescription</key>
<true/>
I have tried adding these permissions in info.plist file but nothing worked. Kindly give me some suggestions how to fix this.
Hi I have solved this issue by my own
I was getting the file saved in my IOS device’s document directory but the file was not getting saved there with .pdf extension so i appended the file extension manually as IOS don’t do it by own so this worked for me i am sharing the solution below.
const downloadReport = async () => {
try {
const { dirs } = ReactNativeBlobUtil.fs;
const dirToSave = LAYOUT.isiOS
? dirs.DocumentDir
: dirs.LegacyDownloadDir;
const date = new Date();
const fileName = `download${Math.floor(
date.getDate() + date.getSeconds() / 2
)}.pdf`;
const config = {
fileCache: true,
addAndroidDownloads: {
useDownloadManager: true,
notification: true,
path: `${dirToSave}/${fileName}`,
description: t("downloading"),
},
path: `${dirToSave}/${fileName}`,
appendExt: "pdf",
};
await ReactNativeBlobUtil.config(config).fetch("GET", FILE_URL, {});
showSuccessMessage(t("fileDownloadedSuccessfully"));
} catch (error) {
showErrorMessage(t("failedToDownloadFile"));
}
};
This is my solution to this problem
Thanx.