2025
06/17
15:30
微信小程序下载文件
微信小程序在下载文件时可能只取到.bin文件,需要对其下载的内容进行转换
手动转换文件格式
Taro.downloadFile({
url: `${baseUrl.url}${downloadUrl}?id=${data.Data.CompanyOfferFilesId}`,
header: {
Authorization: token
},
success: async function (res) {
const filePath = res.tempFilePath;
// 手动重命名为 .docx
const newFilePath = `${Taro.env.USER_DATA_PATH}/offer.docx`; // 小程序特定路径
try {
await Taro.getFileSystemManager().saveFile({
tempFilePath: filePath,
filePath: newFilePath,
success(saveRes) {
Taro.openDocument({
filePath: newFilePath,
fileType: 'docx',
showMenu: true,
success() {
console.log('打开成功');
},
fail(err) {
console.error('打开失败', err);
}
});
},
fail(err) {
console.error('保存失败', err);
}
});
} catch (e) {
console.error('处理文件失败', e);
}
}
});