我尝试了很多东西,但没有任何效果。
我制作了一个下载 .xslx 文件的功能,她可以工作。这是我的功能:
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'));
})
可以看到,我使用RNFetchBlob来下载。一切正常。
但由于某些原因,我无法下载.docx文件。文件下载,出现在文件应用程序中,但我无法打开它。
这是我用来下载 Word 文件的代码:
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'));
})
我使用几乎相同的功能,但文件不正确......有人对此有解决方案吗?