住房和城乡建设部官方网站,天津市网站制作公司,天津城建设计院网站,html网页编程软件一、实现的功能#xff1a;
1、word、excel、图片等实现下载并预览
2、打开文件后显示文件名称
二、代码#xff1a;
// 判断文件类型whatFileType(url) {let sr url.lastIndexOf(.); // 最后一次出现的位置let fileType url.substr(sr 1); // 截取url的…一、实现的功能
1、word、excel、图片等实现下载并预览
2、打开文件后显示文件名称
二、代码
// 判断文件类型whatFileType(url) {let sr url.lastIndexOf(.); // 最后一次出现的位置let fileType url.substr(sr 1); // 截取url的类型return fileType;},// 下载文件/*** 本地文件存储的大小限制为 10M*/handleDownloadFile(e) {let fileTypes [doc, docx, xls, xlsx, ppt, pptx, pdf];let imageTypes [jpg, jpeg, png];let fileType this.whatFileType(e.target.dataset.fileurl);let fileName imageTypes.includes(fileType) ? : e.target.dataset.name;const timestamp Date.parse(new Date());wx.showLoading({title: 加载中,});wx.getSavedFileList({// 获取文件列表success(res) {res.fileList.forEach((val, key) {// 遍历文件列表里的数据// 删除存储的垃圾数据wx.removeSavedFile({filePath: val.filePath,});});},});wx.downloadFile({url: e.target.dataset.fileurl,filePath: fileTypes.includes(fileType)? wx.env.USER_DATA_PATH / timestamp fileName: wx.env.USER_DATA_PATH / . fileType,method: GET,success: function (res) {if (fileTypes.includes(fileType)) {wx.openDocument({filePath: res.filePath,showMenu: true,flieType: fileType,success: function (res) {wx.hideLoading();},fail: function (err) {wx.hideLoading();},});} else if (imageTypes.includes(fileType)) {wx.hideLoading();wx.previewImage({showmenu: true,current: res.filePath, // 当前显示图片的http链接urls: [res.filePath], // 需要预览的图片http链接列表});} else {wx.hideLoading();wx.showModal({title: 提示,content: 文件类型不支持预览,showCancel: false,});}},fail: function (err) {wx.hideLoading();wx.showToast({title: 下载超时,icon: none,});},});},