简单实现小程序的下载功能,具体代码如下:
down:function(){
let that =this
wx.showLoading({
title: '下载中,请稍等...',
})
wx.downloadFile({
url: that.data.imgurl,
success(res){
wx.hideLoading()
that.setData({
tempimgurl :res.tempFilePath
})
// 保存的两种形式1.打开文件,并显示转发按钮
wx.openDocument({
filePath: res.tempFilePath,
showMenu:true,
success(res){
console.log('success')
}
})
// 2.保存到相册
// wx.saveImageToPhotosAlbum({
// filePath: res.tempFilePath,
// success(ress){
// console.log(ress)
// }
// })
}
})
},
下载文件到临时文件后,如果想保存,图片资源的话可以使用wx.saveImageToPhotosAlbum保存到相册,如果其他类型的资源可以使用wx.openDocument, showMenu:true该设置文件右上角会有操作菜单,可以选择保存分享之类的操作。