I've try a lot of things but nothing works.
I make a function to download .xslx file, and she works. This is my function :
const { config, fs } = RNFetchBlob
var token = await getToken();
const link = host + api
let path;
if (Platform.OS === 'ios')
path = fs.dirs.DocumentDir + "/test" + moment().format("DD-MM-YYYY") + ".xlsx"
if (Platform.OS === 'android')
path = fs.dirs.DownloadDir + "test" + moment().format("DD-MM-YYYY") + ".xlsx"
return config({
fileCache: true,
useDownloadManager: true,
notification: true,
path: path,
mime : 'text/plain',
description: 'Downloading file from App',
})
.fetch(requestType, link, {
Authorization: token,
'Content-Type': mimetype_attachment,
})
.then((res) => {
if (Platform.OS === 'android'){
ToastAndroid.show(I18n.t("downloadComplete"), ToastAndroid.LONG)
RNFetchBlob.android.actionViewIntent(path, "application/vnd.ms-excel")
RNFetchBlob.android.addCompleteDownload({
title: I18n.t('downloading'),
description: I18n.t("downloadComplete"),
mime: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
path: path,
showNotification: true,
})
}
else{
Alert.alert(I18n.t('downloading'), I18n.t('downloadComplete'));
}
}).catch((error)=>{
Alert.alert(I18n.t('downloading'), I18n.t('downloadFailed'));
})
As you can see, I use RNFetchBlob to download. Everything works fine.
But for some reasons, I can't download a .docx file. The file download, appear in the files app, but I can't open it.
This is the code I use to download the Word file :
const { config, fs } = RNFetchBlob
var token = await getToken();
const link = host + api
let path;
let directory;
if (Platform.OS === 'ios')
directory = fs.dirs.DocumentDir;
if (Platform.OS === 'android')
directory = fs.dirs.DownloadDir;
path = directory + `/test_${moment().format("YYYY-MM-DD HH:mm:ss")}.docx`
return config({
fileCache: true,
useDownloadManager: true,
notification: true,
path: path,
mime : 'text/plain',
description: 'Downloading file from App',
})
.fetch(requestType, link, {
Authorization: token,
'Content-Type': mimetype_attachment,
})
.then((res) => {
let base64Str = res.base64();
const dirToSave = Platform.OS === 'ios' ? fs.dirs.DocumentDir :
fs.dirs.DownloadDir;
let fLocation = dirToSave + `/test_${moment().format("YYYY-MM-DD
HH:mm:ss")}.docx`
if (Platform.OS === "ios") {
RNFetchBlob.ios.openDocument(res.data);
}
if (Platform.OS === 'android'){
RNFetchBlob.fs.writeFile(fLocation, RNFetchBlob.base64.encode(base64Str),
'base64');
ToastAndroid.show(I18n.t("downloadComplete"), ToastAndroid.LONG)
RNFetchBlob.android.addCompleteDownload({
title: I18n.t('downloading'),
description: I18n.t("downloadComplete"),
mime: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
path: path,
showNotification: true,
})
}
else{
Alert.alert(I18n.t('downloading'), I18n.t('downloadComplete'));
}
}).catch((error)=>{
Alert.alert(I18n.t('downloading'), I18n.t('downloadFailed'));
})
I use pratically the same function, but the file is not ok... Someone has a solution about this ?