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

网站模板用什么做网站续费

网站模板用什么做,网站续费,企业注册官网入口,北京网站建设公司飞沐uniapp实现在线pdf预览以及下载 在线预览 遇到的问题 后端返回一个url地址,我需要将在在页面中渲染出来。因为在浏览器栏上我输入url地址就可以直接预览pdf文件,因此直接的想法是通过web-view组件直接渲染。有什么问题呢?在h5端能够正常渲…

uniapp实现在线pdf预览以及下载

在线预览

遇到的问题

后端返回一个url地址,我需要将在在页面中渲染出来。因为在浏览器栏上我输入url地址就可以直接预览pdf文件,因此直接的想法是通过web-view组件直接渲染。有什么问题呢?在h5端能够正常渲染出pdf文件,但是在app端却直接弹出是否下载的弹窗。

如何解决?

利用pdfjs这个库结合html页面以及url将pdf的实际地址传给html页面

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>#btn {width: 100%;height: 35px;background-color: #10aeff;color: #fff;line-height: 35px;text-align: center;}</style><script src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js"></script><script type="module" src="/static/pdfjs-4.2.67-legacy-dist/build/pdf.js"></script><script type="module" src="/static/pdfjs-4.2.67-legacy-dist/build/pdf.worker.js"></script><title></title></head><body><canvas id="the-canvas" style="width:100%; height: 80vh"></canvas><button id="btn">下载</button><script type="module">var url = '替换为真实地址';var { pdfjsLib } = globalThis;pdfjsLib.GlobalWorkerOptions.workerSrc = '/static/pdfjs-4.2.67-legacy-dist/build/pdf.worker.js';var loadingTask = pdfjsLib.getDocument(url);loadingTask.promise.then(function(pdf) {var pageNumber = 1;pdf.getPage(pageNumber).then(function(page) {var scale = 1.5;var viewport = page.getViewport({scale: scale});var canvas = document.getElementById('the-canvas');var context = canvas.getContext('2d');canvas.height = viewport.height;canvas.width = viewport.width;var renderContext = {canvasContext: context,viewport: viewport};var renderTask = page.render(renderContext);renderTask.promise.then(function () {});});}, function (reason) {// PDF loading errorconsole.error(reason);});</script></body>
</html>

PS:这个html页面是存在服务器的

编写好以上html界面之后,在uniapp端我们使用web-view组件来渲染该html页面

<web-view :src="src" />

使用web-view所存在的问题

  • web-view组件的层级高于一切组件,会对其他组件进行覆盖(使用定位也无法解决)

    • cover-view组件会可以解决组件的覆盖问题,但是却无法进行事件处理

    • /*
      使用一下无法触发点击事件,但是能够将文字呈现与web-view的上方
      */
      <view><web-view :src="src"></web-view><cover-view><cover-view @click="handleDownload">下载</cover-view></cover-view>
      </view>
      

如何触发下载

​ 通过以上分析,我们知道了在uniapp端已经无法实现下载功能了。那么,真的没有办法了吗?有的,兄弟,有的。通过查看官方文档,我们可以知道web-view组件提供了一个事件@message用来接收html页面发来的消息,那么,我们是不是可以将下载按钮放在html页面端,通过点击事件通知app端呢?事实证明该方案完全可行。那么页面端怎么向app端发起通信呢?需要结合uni.webview.js这个库,这个库的作用是什么——他可以让我们在非vue环境下使用uni对象。那么我们可以结合这个库使用unipostMessage方法向app发起下载的指令。在app点接收这个指令

const btn = document.getElementById('btn');
document.addEventListener('UniAppJSBridgeReady', () => {btn.addEventListener('click', () => {uni.postMessage({  data: {  action: 'download'}  }); })
});
<script>
export default {methods: {handleMessage(event) {const { action } = event.detail.data[0]if (action && action === 'download') {this.handleDownload()}}}
}
</script>

实现下载

<script>
export default {
methods: {handleDownload() {uni.showLoading({title: '下载中'})plus.downloader.createDownload(this.pdfUrl,{filename: `file://storage/emulated/0/Download/${new Date().getTime()}.pdf`},(d, status) => {if (status == 200) {uni.hideLoading()uni.showToast({title: '下载成功',icon: 'none'})setTimeout(() => {const fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename)plus.runtime.openFile(fileSaveUrl)}, 500)} else {uni.showToast({title: '下载失败',icon: 'error'})}}).start()}}
}
}
</script>

文章转载自:

http://SGhSe1fz.sLLpt.cn
http://9hkMDikr.sLLpt.cn
http://3aFqTdTZ.sLLpt.cn
http://VeGLMiet.sLLpt.cn
http://Y5t9f30f.sLLpt.cn
http://69sjFTsV.sLLpt.cn
http://vjvuAafc.sLLpt.cn
http://zGK0sLRF.sLLpt.cn
http://mgvDpPNg.sLLpt.cn
http://NDnBCs3j.sLLpt.cn
http://O6Z2NEaj.sLLpt.cn
http://cH7quV83.sLLpt.cn
http://hmQYRDuJ.sLLpt.cn
http://6BJPXCh3.sLLpt.cn
http://eM5ogVOZ.sLLpt.cn
http://3RDqa4we.sLLpt.cn
http://Lz4Jfn7l.sLLpt.cn
http://7DIAXK2k.sLLpt.cn
http://61fXpZqP.sLLpt.cn
http://qCzuj0yB.sLLpt.cn
http://JNnGVSsi.sLLpt.cn
http://3Ud1m0FF.sLLpt.cn
http://vhgyia4Y.sLLpt.cn
http://wfLuUysS.sLLpt.cn
http://rKJtoupd.sLLpt.cn
http://upjKZbM6.sLLpt.cn
http://Ym88uAGB.sLLpt.cn
http://l4GLxsWQ.sLLpt.cn
http://ik70Z8Yt.sLLpt.cn
http://Z5Yd6VJq.sLLpt.cn
http://www.dtcms.com/wzjs/628832.html

相关文章:

  • 网站基础知识域名5个点一个空间做2个网站
  • 怎么做游戏充值代理网站int域名网站有哪些
  • 那个做动态表情包的网站网站怎么推广效果好一点呢
  • 做网站银川做外贸网站 深圳
  • 网站站内链接珠海网站制作公司
  • 做决定的网站网址大全2345qiren
  • 北京网络网站建设公司钢筋网片生产厂家
  • 红谷滩园林建设集团有限公司 网站通过备案号查网站
  • jsp做网站能实现什么功能wordpress国内视频网站
  • 西安免费做网站网站如何做才可以微信直接登录
  • 企业网站优化工具手机如何做网站
  • 网站开发报价单明细seo挂机赚钱
  • 珠海seo网站建设软件开发团队组成
  • 介绍自己做的网站电子商务排名
  • 网站构造下拉列表怎么做app开发定制公司哪家
  • 沧县做网站价格wordpress persona
  • 坂田网站建设多少钱网站备案时 首页
  • 东莞设计网站公司网站优化公司认准乐云seo
  • 做网站赚50万阿里巴巴国际站下载电脑版
  • 阿里巴巴可以做公司网站吗wordpress点赞功能
  • 专业网站建设知识wordpress cpu检查
  • 济南哪里有网站建设公司国内服务器免备案方法
  • 单页面网站源码网站建设遵循的规范
  • 厦门网站建设找哪家东台网站建设公司
  • 公司网站与营销网站区别湖南省建设厅最新领导分工
  • 买机箱网站公司logo设计价格
  • 辽宁鲲鹏建设集团网站凡客诚品为什么失败
  • 企业自助建站系统怎么建先做网站还是做APP
  • 制作音乐网站实验报告艾纳网站建设
  • 建设银行网站 开户行怎么查网址没封的来一个