//普通文件
const url = 'https://img.36krcdn.com/20200411/v2_16417a06088947debe0450950f8fc813_img_png'
// 下载二进制文件
down() {
this.reqByToken({
method: 'GET',
url: `${this.configs.file_url}download/?ID=39287484`, //请求文件流地址
responseType: "arraybuffer",
success: rest => {
if (rest.statusCode === 200) {
const cd = rest.header['Content-Disposition'],
filename = cd.match(/"(\S*)"/)[1]
//获取全局唯一的文件管理器
const fs = wx.getFileSystemManager()
//wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
const filePath = `${wx.env.USER_DATA_PATH}/${filename}`
// 写文件
fs.writeFile({
filePath,
data: rest.data,
encoding: "binary", //二进制流文件必须是 binary
success: (res) => {
this.handleFile(filePath)
console.log(111, 'down', res, filePath)
}
});
}
}
});
},
// 下载普通文件
down1() {
uni.downloadFile({
url,
success: (res) => {
if (res.statusCode === 200) {
this.handleFile(res.tempFilePath)
console.log(222, 'down1', res)
}
}
})
},
// 对不同文件的处理
handleFile(filePath) {
const filetype = filePath.split('.')[1],
typeObj = {
gif: 'img',
GIF: 'img',
png: 'img',
PNG: 'img',
jpg: 'img',
JPG: 'img',
jpeg: 'img',
mp4: 'video',
doc: 'doc',
docx: 'doc',
xls: 'doc',
xlsx: 'doc',
ppt: 'doc',
pptx: 'doc',
pdf: 'doc'
},
result = (ok = '成功保存到相册', no = '保存失败') => {
return {
filePath,
success: res => {
uni.showToast({
title: ok
})
},
fail: err => {
uni.showToast({
title: no
})
}
}
}
if (typeObj[filetype] === 'video') {
// 保存视频到系统相册:mp4
uni.saveVideoToPhotosAlbum(result())
} else if (typeObj[filetype] === 'img') {
// 保存图片到系统相册:gif,jpg,jpeg,png,GIF,JPG,PNG
uni.saveImageToPhotosAlbum(result())
} else {
// 打开文件:doc,docx,xls,xlsx,ppt,pptx,pdf
uni.openDocument({
...result('打开文档成功', '打开文档失败'),
showMenu: true//showMenu是否显示右上角菜单
});
}
}
down() {
this.reqByToken({
method: 'GET',
url: `${this.configs.file_url}download/?ID=${this.info.attachmentId}`, //请求文件流地址
responseType: "arraybuffer",
success: rest => {
if (rest.statusCode === 200) {
const cd = rest.header['Content-Disposition'],
filename = cd.match(/"(\S*)"/)[1]
//获取全局唯一的文件管理器
const fs = wx.getFileSystemManager()
//wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
const filePath = `${wx.env.USER_DATA_PATH}/${filename}`
// 写文件
fs.writeFile({
filePath,
data: rest.data,
encoding: "binary", //二进制流文件必须是 binary
success: (res) => {
this.handleFile(filePath)
}
});
}
}
});
},
网友评论