message-file.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <div class="message-file">
  3. <div class="box" @click="download" :title="$t('TUIChat.单击下载')">
  4. <i class="icon icon-files"></i>
  5. <div class="message-file-content">
  6. <label>{{ data.name }}</label>
  7. <span>{{ data.size }}</span>
  8. </div>
  9. </div>
  10. <progress v-if="data.progress" :value="data.progress" max="1"></progress>
  11. </div>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent, watchEffect, reactive, toRefs } from 'vue';
  15. export default defineComponent({
  16. props: {
  17. data: {
  18. type: Object,
  19. default: () => ({}),
  20. },
  21. },
  22. setup(props: any, ctx: any) {
  23. const data = reactive({
  24. data: {},
  25. });
  26. watchEffect(() => {
  27. data.data = props.data;
  28. });
  29. const download = () => {
  30. const file: any = data.data;
  31. const option: any = {
  32. mode: 'cors',
  33. headers: new Headers({
  34. 'Content-Type': 'application/x-www-form-urlencoded',
  35. }),
  36. };
  37. // 浏览器支持fetch则用blob下载,避免浏览器点击a标签,跳转到新页面预览的行为
  38. // If the browser supports fetch, use blob to download, so as to avoid the browser clicking the a tag and jumping to the preview of the new page
  39. if ((window as any).fetch) {
  40. fetch(file.url, option)
  41. .then((res) => res.blob())
  42. .then((blob) => {
  43. const a = document.createElement('a');
  44. const url = window.URL.createObjectURL(blob);
  45. a.href = url;
  46. a.download = file.name;
  47. a.click();
  48. });
  49. } else {
  50. const a = document.createElement('a');
  51. a.href = file.url;
  52. a.target = '_blank';
  53. a.download = file.name;
  54. a.click();
  55. }
  56. };
  57. return {
  58. ...toRefs(data),
  59. download,
  60. };
  61. },
  62. });
  63. </script>
  64. <style lang="scss" scoped>
  65. @import url('../../../styles/common.scss');
  66. @import url('../../../styles/icon.scss');
  67. .message-file {
  68. display: flex;
  69. flex-direction: column;
  70. .box {
  71. flex: 1;
  72. display: flex;
  73. cursor: pointer;
  74. .message-file-content {
  75. flex: 1;
  76. display: flex;
  77. flex-direction: column;
  78. }
  79. }
  80. progress {
  81. width: 100%;
  82. color: #006eff;
  83. appearance: none;
  84. border-radius: 0.25rem;
  85. background: rgba(#ffffff, 1);
  86. width: 100%;
  87. height: 0.5rem;
  88. &::-webkit-progress-value {
  89. background-color: #006eff;
  90. border-radius: 0.25rem;
  91. }
  92. &::-webkit-progress-bar {
  93. border-radius: 0.25rem;
  94. background: rgba(#ffffff, 1);
  95. }
  96. &::-moz-progress-bar {
  97. color: #006eff;
  98. background: #006eff;
  99. border-radius: 0.25rem;
  100. }
  101. }
  102. }
  103. </style>