wx.saveFile和wx.getFileSystemManager().saveFile的区别
wx.saveFile之后调用wx.openDocument得到的是临时文件地址和临时文件名,原文件名就没了,所以先保存原文件名。再用wx.getFileSystemManager().saveFile将文件名替换回来
downLoad1: function(e) {
let dataset = e.currentTarget.dataset
let idx = dataset.idx;
var that = this;
let uri = that.data.beforeList[idx].fileUri;
let fileName = that.data.beforeList[idx].fileName;
wx.downloadFile({
url: uri,
success: function(res) {
console.log(res);
const manage = wx.getFileSystemManager();
if (res.statusCode === 200) {
manage.saveFile({
tempFilePath: res.tempFilePath,
filePath: wx.env.USER_DATA_PATH + "/" + fileName,
success: function(res) {
}
});
// 打开文档
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + "/" + fileName,
success: function(res) {
console.log('打开文档成功')
},
fail: function() {
console.log('打开失败');
}
})
}
// // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
// if (res.statusCode === 200) {
// // 将临时地址转存到本地缓存中
// wx.saveFile({
// tempFilePath: res.tempFilePath,
// success: function(res) {
// console.log(res);
// var savedFilePath = res.savedFilePath;
// console.log('文件已下载到' + savedFilePath);
// // 查看下载的文件列表
// wx.getSavedFileList({
// success: function(res) {
// console.log(res);
// }
// })
// // 打开文档
// wx.openDocument({
// filePath: savedFilePath,
// success: function(res) {
// console.log('打开文档成功')
// },
// fail: function() {
// console.log('打开失败');
// }
// })
// }
// })
// }
}
})
},
注释部分的问题就是下载文件打开后文件名有问题,且文件类型也不对