一、第一種方式:(好像在ios系統(tǒng)上不兼容)
1.1實(shí)現(xiàn)效果:點(diǎn)擊文件附件,下載到本地
1.2具體代碼:
<view class="contact" @click="downloadFun(item.enclosure)">點(diǎn)擊下載</view>
// 下載附件
downloadFun(enclosure){
var that = this
//加載框動(dòng)畫
uni.showLoading({title: '正在下載……'});
// console.log(that.hostUrl + '/uploads'+ enclosure)
uni.downloadFile({
url: that.hostUrl + '/uploads'+ enclosure,//下載地址接口返回
success: (data) => {
console.log('打印data',data)
if (data.statusCode === 200) {
//隱藏加載框
uni.hideLoading();
//文件保存到本地
uni.saveFile({
tempFilePath: data.tempFilePath, //臨時(shí)路徑
success: function(res) {
// console.log('打印res',res)
uni.showToast({
icon: 'success',
mask: true,
// title: '文件已保存:' + res.savedFilePath, //保存路徑
title: '下載成功' ,
duration: 2000,
});
//自動(dòng)打開文檔查看
setTimeout(() => {
var filePath = res.savedFilePath;
uni.openDocument({ //新開頁(yè)面打開文檔,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
filePath: filePath,
showMenu: true,
success: function (res) {
console.log('打開文檔成功');
}
});
}, 2000)
}
});
}
},
fail: (err) => {
console.log(err);
uni.showToast({
icon: 'none',
mask: true,
title: '文件下載失敗',
});
},
});
},
———————————————————分割線————————————————————— ———————————————————分割線————————————————————— ———————————————————分割線————————————————————— ———————————————————分割線—————————————————————
二、第二種方式:
2.1:效果圖
保存方式是:點(diǎn)擊下載按鈕,通過(guò)微信選擇一個(gè)好友,發(fā)給給好友的方式,進(jìn)行保存。
2.2實(shí)現(xiàn)代碼:
下載按鈕:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-575005.html
<image @click="downloadsFun(infos.file,infos.id)" src="../../static/images/icon_download@2x.png" mode=""></image>
js:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-575005.html
// 文件下載預(yù)覽轉(zhuǎn)發(fā)
downloadsFun(myfile, myid){
var that = this;
uni.showLoading({
title: '正在下載……'
});
wx.downloadFile({
url:that.hostUrl + myfile, // 下載url
// filePath: wx.env.USER_DATA_PATH,
success (res) {
console.log('ressss',res)
if(res.statusCode == 200) {
//隱藏加載框
uni.hideLoading();
console.log('res.statusCode',res)
// that.openFileEvs(res)
wx.shareFileMessage({
filePath: res.tempFilePath,
// fileType: 'xlsx',
success(data) {
console.log('轉(zhuǎn)發(fā)成功!??!',data)
uni.showToast({
icon: 'success',
mask: true,
// title: '文件已保存:' + res.savedFilePath, //保存路徑
title: '轉(zhuǎn)發(fā)成功',
duration: 2000,
});
},
// fileName:'自定義文件名字.xlsx',
fail: console.error,
})
}
},
// fileName:'導(dǎo)出的文件名',
fail: console.error,
})
},
ending~
到了這里,關(guān)于【下載文件】uniapp開發(fā)小程序,下載文件并保存到本地的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!