preview-file.vue 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <view>
  3. <template v-if="isPic">
  4. <view class="file-name" v-if="showName">{{ name }}</view>
  5. <image class="img" @click="previewImg" :src="src" mode="aspectFill"></image>
  6. </template>
  7. <view class="file-name" v-if="!isPic" @click="previewFile">{{ name }}</view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. props: ["name", "src", "showName"],
  13. computed: {
  14. isPic() {
  15. var reg = /\.(png|jpg|gif|jpeg|webp)$/;
  16. return reg.test(this.name);
  17. },
  18. },
  19. methods: {
  20. previewImg() {
  21. uni.previewImage({
  22. current: 0,
  23. urls: [this.src],
  24. });
  25. },
  26. previewFile() {
  27. console.log(this.src);
  28. uni.showLoading({
  29. title: "下载中...",
  30. mask: true,
  31. });
  32. uni.downloadFile({
  33. url: this.src,
  34. success: (res) => {
  35. if (res.statusCode === 200) {
  36. uni.openDocument({
  37. filePath: res.tempFilePath,
  38. // filePath: escape(res.tempFilePath),
  39. // 如果文件名包含中文,建议使用escape(res.tempFilePath)转码,防止ios和安卓客户端导致的差异
  40. success: function(res) {
  41. console.log("打开文档成功");
  42. },
  43. });
  44. }
  45. },
  46. complete: () => {
  47. uni.hideLoading();
  48. },
  49. });
  50. },
  51. },
  52. };
  53. </script>
  54. <style lang="scss" scoped>
  55. .img {
  56. width: 100rpx;
  57. height: 100rpx;
  58. }
  59. .file-name {
  60. font-size: 30rpx;
  61. font-weight: bold;
  62. color: #2f42ca;
  63. text-decoration: underline;
  64. }
  65. </style>