<view class="name" @click="onDownload(item)">
{{item.name}}
</view>
首先请求文件流
onDownload(media) {
uni.downloadFile({
//只能是GET请求
url: constants.VUE_APP_API + constants.AVATAR_NAME + media.uuid, //请求地址
success: res => {
//下载成功
if (res.statusCode === 200) {
let filePath = res.tempFilePath;
this.download(filePath, media.name)
uni.showToast({
title: `文件下载成功`,
icon: 'none'
});
}
},
fail: e => {
uni.showToast({
title: `文件下载失败` + e,
icon: 'none'
});
}
});
},
创建a标签 下载文件
download(href, title) {
const a = document.createElement('a');
a.setAttribute('href', href);
a.setAttribute('download', title);
a.click();
},