微信小程序下载文件(非图片),并校验扩展名。

 


downloadFile: function(e) { wx.showLoading({ title: '下载中...', }); var url = e.currentTarget.dataset.info;//下载文件后端路径 //获取文件后缀 let ext = url.substr(url.lastIndexOf(".")).replace('.', ''); if (ext != 'doc' && ext != 'docx' && ext != 'xls' && ext != 'xlsx' && ext != 'ppt' && ext != 'pptx' && ext != 'pdf' && ext != 'rar' && ext != 'zip') { wx.hideLoading(); app.showToast({ title: '错误的文件格式', icon: 'none' }) }; //下载文件到缓存 wx.downloadFile({ url: url, success(res) { const filePath = res.tempFilePath; //保存文件到本地缓存文件 wx.saveFile({ tempFilePath: filePath, success(res) { wx.hideLoading(); app.showToast({ title: '下载成功', icon: 'none' }) //下载的文件路径 const savedFilePath = res.savedFilePath; console.log(res) } }) } }) },

 

posted @ 2020-10-13 14:08  万斗司  阅读(687)  评论(0编辑  收藏  举报