// 文件下载,方案一:直接下载文件
export const downLoadFile = (link: string) => {
wx.downloadFile({
url: link,
success(res) {
console.log('file:', res)
if (res.statusCode === 200) {
const filePath = res.tempFilePath
wx.openDocument({
filePath: filePath,
showMenu: true, //显示分享按钮
success: function () {
wx.hideLoading()
}
})
}
}
})
}
// 导出文件方案二:
export const downLoadBuffer = (buffer: any) => {
const fs = wx.getFileSystemManager()
let filePath = `${wx.env.USER_DATA_PATH}/hello.doc`
fs.writeFile({
filePath: filePath,
data: buffer,
encoding: 'base64',
success(res) {
wx.openDocument({
filePath: filePath,
showMenu: true, //显示分享按钮
success: function () {
wx.hideLoading()
}
})
console.log('res:', res)
},
fail(res) {
console.error("errorxxx:", res)
}
})
}
// 导出文件方案三
export const downLoad = () => {
wx.request({
url: 'http://10.30.34.159:9204/order/export/word',
method: 'GET',
header: {
'content-type': 'application/octet-stream', // 默认值
},
responseType: 'arraybuffer',
success(res) {
console.log('res:', res)
let buffer = res.data
const fs: any = wx.getFileSystemManager()
let filePath = `${wx.env.USER_DATA_PATH}/hello.doc`
fs.writeFile({
filePath: filePath,
data: buffer,
encoding: 'base64',
success(res: any) {
wx.openDocument({
filePath: filePath,
showMenu: true, //显示分享按钮
success: function () {
wx.hideLoading()
}
})
console.log('res:', res)
},
fail(res: any) {
console.error("errorxxx:", res)
}
})
},
fail(err) {
console.log('err:', err)
}
})
}