使用 api -> uni.openDocument(OBJECT)

image.png

  1. // 实例代码
  2. uni.downloadFile({
  3. url: 'https://example.com/somefile.pdf',
  4. success: function (res) {
  5. var filePath = res.tempFilePath;
  6. uni.openDocument({
  7. filePath: filePath,
  8. success: function (res) {
  9. console.log('打开文档成功');
  10. }
  11. });
  12. }
  13. });

——————————————————————demo ———————————————————————————————————

  1. <template>
  2. <u-image @longpress="handleLongpress()" @click="documentClick" width="100%" height="800rpx" :src="`${$public()}/helpCenter/company.png`" />
  3. </template>
  4. <script>
  5. export default {
  6. data:()=>({
  7. docUuid:'',
  8. docUuidUrl:'',
  9. format:'pdf',
  10. }),
  11. methods:{
  12. getFileExtensionFn() { // 获取文件拓展名
  13. getFileExtension({
  14. uuid: this.docUuid,
  15. }).then(re => {
  16. if (re.success) {
  17. this.format = re.data
  18. console.log("11", this.format)
  19. }
  20. });
  21. },
  22. getUrlByUuidFnDownUrl(uuid) { //获得文档的下载地址
  23. getUrlByUuid({
  24. uuid: this.docUuid
  25. }).then(re => {
  26. if (re.success) {
  27. this.docUuidUrl = re.data
  28. }
  29. });
  30. },
  31. async handleLongpress() {
  32. var fileType = this.format;
  33. uni.downloadFile({
  34. // url: this.docUuidUrl,
  35. url:'https://pmbimcloud-test-company.oss-cn-hangzhou.aliyuncs.com/ad43011a-d8ad-2c92-8686-01991893b03b.pdf?Expires=1627451113&OSSAccessKeyId=LTAI8PPrIEVEAQCK&Signature=v9i3VL1Ir0hFHP3dNK%2BXTG1%2BcVc%3D',
  36. success: function(res) {
  37. var filePath = res.tempFilePath;
  38. uni.openDocument({
  39. filePath: filePath,
  40. fileType: fileType,
  41. success: function(res) {
  42. console.log('打开文档成功');
  43. },
  44. });
  45. }
  46. });
  47. },
  48. documentClick() {
  49. this.$u.route({
  50. url: 'pages/supervisionThroughTrain/pages/meetingDetail/documentView'
  51. })
  52. },
  53. }
  54. }
  55. </script>
  1. // documentView 文件 使用 web-view 在线查看文档
  2. <template>
  3. <view>
  4. <web-view :webview-styles="webviewStyles" :src="urls"></web-view>
  5. </view>
  6. </template>
  7. <script>
  8. import {
  9. getUrlByUuid
  10. } from '@/pages/supervisionThroughTrain/utils/meetingDetailApi.js';
  11. export default {
  12. data() {
  13. return {
  14. urls: '',
  15. webviewStyles: {
  16. progress: {
  17. color: '#FF3333'
  18. }
  19. }
  20. }
  21. },
  22. methods: {
  23. getUrlByUuidFn() { //获取文档浏览地址
  24. this.urls=`https://uniapp.dcloud.io/api/media/image?id=unipreviewimageobject`;
  25. },
  26. },
  27. onLoad(){
  28. this.getUrlByUuidFn()
  29. }
  30. }
  31. </script>

image.png
长按 ->
image.png

注 : @longpress 是 uni-app 自带的长按方法

点击 -> 跳转到 documentView界面
image.png