preview.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // 允许打开的文件类型
  2. const typeList = ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf']
  3. // 文件预览
  4. export function preview (url) {
  5. uni.showLoading({
  6. title: '文件加载中...'
  7. })
  8. wx.downloadFile({
  9. url,
  10. success: function (res) {
  11. const filePath = res.tempFilePath
  12. const arr = res.tempFilePath.split('.')
  13. const fileType = arr[1]
  14. if (!typeList.includes(fileType)) {
  15. uni.showToast({
  16. icon: "none",
  17. title: '暂未支持此类型的文件查看',
  18. duration: 2000,
  19. })
  20. return
  21. }
  22. wx.openDocument({
  23. filePath: filePath,
  24. fileType,
  25. showMenu: true, //显示右上角菜单
  26. success: function (res) {
  27. console.log(res, "openDocument")
  28. console.log("打开文档成功")
  29. },
  30. fail: res => {
  31. uni.showToast({
  32. icon: 'none',
  33. title: res.errMsg,
  34. duration: 5000
  35. })
  36. }
  37. })
  38. },
  39. fail: (res) => {
  40. uni.showToast({
  41. icon: 'none',
  42. title: res.errMsg,
  43. duration: 5000
  44. })
  45. },
  46. complete: res => {
  47. uni.hideLoading()
  48. }
  49. })
  50. }