当前位置: 首页 > news >正文

预览和下载 (pc和微信小程序)

1.微信小程序 预览pdf 或者 图片等 

//utils.js  文件//通过接口返回文件链接 打开文档
export default function previewFile({ downLinkUrl, tempFilePath }) {let url = "https://" + downLinkUrl.replace("http://", "").replace("https://", "");return new Promise((resolve, reject) => {wx.downloadFile({url: url,success(res) {//保存到本地wx.saveFile({tempFilePath: res.tempFilePath,success(res) {const savedFilePath = res.savedFilePath;// 打开文件wx.openDocument({filePath: savedFilePath,showMenu: true,success(res) {resolve({ code: 200, msg: "打开文档成功", data: { savedFilePath } });},fail(err) {reject({ code: 500, msg: "打开文件失败", data: err });}});},fail(err) {reject({ code: 500, msg: "保存文件失败", data: err });}});},fail(err) {reject({ code: 500, msg: "下载文件失败", data: err });}});});
}
 //预览       previewFile(item) {//图片类型const isImageType = [".png", ".jpe", ".jpeg", ".jpg", ".gif"];const fileSuffix = item.fileName.substring(item.fileName.lastIndexOf("."));//如果是图片if (isImageType.includes(fileSuffix)) {wx.previewImage({showmenu: true,urls: [item.downLinkUrl], // 需要预览的图片 http 链接列表success() {console.log("打开图片成功");},fail(err) {console.log(err);uni.$u.toast("打开图片失败");}});} else {uni.showLoading();//如果有临时文件路径if (item.tempFilePath) {// 打开文件wx.openDocument({filePath: item.tempFilePath,showMenu: true,success(res) {uni.$u.toast("打开文档成功");},fail(err) {uni.$u.toast("预览失败请稍后重试");}});} else {previewFile(item).then(res => {let { savedFilePath } = res.data;item.tempFilePath = savedFilePath;uni.$u.toast(res.msg);}).catch(err => {uni.$u.toast(err.msg);}).finally(() => {// uni.hideLoading();});}}},//判断是否显示预览按钮isPreviewFileType(fileName) {const fileSuffix = fileName.substring(fileName.lastIndexOf("."));const isPreviewFileType = [".png",".jpe",".jpeg",".jpg",".gif",".pdf",".PDF",".doc",".docx",".dot",".xlc",".xlm",".xls",".xlt",".xlw",".xlsx"];if (isPreviewFileType.includes(fileSuffix)) {return true;} else {return false;}}

文件下载

        downLoadFile(item) {if (item.downLoading) return;item.downLoading = true;const linkUrl = item.downLinkUrl.replace(/^http:\/\//, "https://");uni.downloadFile({url: linkUrl,success: res => {item.downTempFilePath = res.tempFilePath;this.onExport(item);},fail: err => {console.log("err----", err);this.$u.toast("下载失败,请检查您的网络");},complete: function() {setTimeout(() => {item.downLoading = false;}, 0);}});},

 2.浏览器预览

export function previewHandle(params) {//新开页面-预览文件let url = params.downLinkUrl,fileName = params.fileName;const fileSuffix = fileName.substring(fileName.lastIndexOf("."));if (isPreviewFileType.includes(fileSuffix)) {const previewUrl = `http://dcsapi.com/?k=57170800621178060806081&url=${encodeURIComponent(url)}&watermark=千里马招标网`;window.open(previewUrl);} else {alert("该文件暂不支持预览~");}
}

http://www.lryc.cn/news/506983.html

相关文章:

  • 使用 DeepSpeed 微调 OPT 基础语言模型
  • BSM和BMS什么区别?
  • 使用Maven打包javaagent.jar
  • R语言混合模型回归GBTM群组轨迹模型绘图可视化研究
  • 【毕业设计】A079-基于Java的影院订票系统的设计与实现
  • Vue.js前端框架教程11:Vue监听器watch和watchEffect
  • 疾风大模型气象系统:精准预报,引领未来
  • U9应收单拉单生成时找不到退货单
  • 设计模式--单例模式【创建型模式】
  • 挑战一个月基本掌握C++(第七天)了解指针,引用,时间,输入输出,结构体,vector容器,数据结构 - 通用完结
  • 百度面试手撕 go context channel部分学习
  • Spring事务管理详解
  • 社区版 IDEA 开发webapp 配置tomcat
  • 打 印 菱 形
  • ffmpeg翻页转场动效的安装及使用
  • [RocketMQ] 发送重试机制与消费重试机制~
  • 基于Redis的网关鉴权方案与性能优化
  • 计算机网络-L2TP VPN基础概念与原理
  • Node.js day-01
  • vue el-dialog实现可拖拉
  • go配置文件
  • C++ OpenGL学习笔记(2、绘制橙色三角形绘制、绿色随时间变化的三角形绘制)
  • 项目搭建+删除(单/批)
  • 《小米创业思考》
  • 多种注意力机制详解及其源码
  • VMWare 的克隆操作
  • Y3编辑器教程7:界面编辑器
  • 「Mac畅玩鸿蒙与硬件45」UI互动应用篇22 - 评分统计工具
  • run postinstall error, please remove node_modules before retry!
  • 详细解读TISAX认证的意义