index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <el-dialog
  3. class="show-file"
  4. :visible="dialogVisibleShowFile"
  5. width="900px"
  6. title="查看文件"
  7. @close="dialogShowFileClose"
  8. >
  9. <template v-if="fileType === 'pdf'">
  10. <pdf v-for="i in numPages" :key="i" :src="pdfSrc" :page="i" />
  11. </template>
  12. <template v-else-if="isImage(fileType)">
  13. <div class="image-parent">
  14. <el-image fit="contain" :src="fileUrl" />
  15. </div>
  16. </template>
  17. <template v-else>
  18. <iframe
  19. :src="'https://view.officeapps.live.com/op/view.aspx?src=' + `${fileUrl}`"
  20. width="100%"
  21. height="490px"
  22. scrolling="no"
  23. />
  24. </template>
  25. <div slot="footer">
  26. <el-button @click="dialogShowFileClose">关闭</el-button>
  27. </div>
  28. </el-dialog>
  29. </template>
  30. <script>
  31. import pdf from 'vue-pdf';
  32. export default {
  33. components: { pdf },
  34. props: {
  35. fileName: {
  36. default: '',
  37. type: String
  38. },
  39. fileUrl: {
  40. default: '',
  41. type: String
  42. }
  43. },
  44. data() {
  45. return {
  46. dialogVisibleShowFile: false,
  47. pdfSrc: '',
  48. numPages: 1
  49. };
  50. },
  51. computed: {
  52. fileType() {
  53. return this.fileName.slice(this.fileName.lastIndexOf('.') + 1, this.fileName.length);
  54. }
  55. },
  56. methods: {
  57. getNumPages(url) {
  58. let loadingTask = pdf.createLoadingTask(url);
  59. loadingTask.promise
  60. .then(pdf => {
  61. this.pdfSrc = loadingTask;
  62. this.numPages = pdf.numPages;
  63. })
  64. .catch(err => {
  65. console.error('pdf加载失败', err);
  66. this.$message.error('pdf加载失败');
  67. });
  68. },
  69. showDialog() {
  70. this.dialogVisibleShowFile = true;
  71. },
  72. dialogShowFileClose() {
  73. this.dialogVisibleShowFile = false;
  74. },
  75. isImage(type) {
  76. return ['jpeg', 'gif', 'jpg', 'png', 'bmp', 'pic', 'svg'].includes(type);
  77. }
  78. }
  79. };
  80. </script>
  81. <style lang="scss">
  82. @import '~@/styles/mixin.scss';
  83. .show-file {
  84. @include dialog;
  85. .image-parent {
  86. display: flex;
  87. justify-content: center;
  88. }
  89. }
  90. </style>