message-file.vue 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div
  3. class="file-message-montainer"
  4. :title="TUITranslateService.t('TUIChat.单击下载')"
  5. @click="download"
  6. >
  7. <Icon
  8. :file="files"
  9. class="file-icon"
  10. />
  11. <div>
  12. <div>{{ props.content.name }}</div>
  13. <div>{{ props.content.size }}</div>
  14. </div>
  15. </div>
  16. </template>
  17. <script lang="ts" setup>
  18. import { withDefaults } from '../../../../adapter-vue';
  19. import { TUITranslateService, IMessageModel } from '@tencentcloud/chat-uikit-engine';
  20. import Icon from '../../../common/Icon.vue';
  21. import files from '../../../../assets/icon/files.png';
  22. import type { IFileMessageContent } from '../../../../interface';
  23. const props = withDefaults(
  24. defineProps<{
  25. content: IFileMessageContent;
  26. messageItem: IMessageModel;
  27. }>(),
  28. {
  29. content: () => ({} as IFileMessageContent),
  30. messageItem: () => ({} as IMessageModel),
  31. },
  32. );
  33. const download = () => {
  34. if (props.messageItem.hasRiskContent) {
  35. return;
  36. }
  37. const option = {
  38. mode: 'cors',
  39. headers: new Headers({
  40. 'Content-Type': 'application/x-www-form-urlencoded',
  41. }),
  42. } as RequestInit;
  43. // 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
  44. if ((window as any)?.fetch) {
  45. fetch(props.content.url, option)
  46. .then(res => res.blob())
  47. .then((blob) => {
  48. const a = document.createElement('a');
  49. const url = window.URL.createObjectURL(blob);
  50. a.href = url;
  51. a.download = props.content.name;
  52. a.click();
  53. });
  54. } else {
  55. const a = document.createElement('a');
  56. a.href = props.content.url;
  57. a.target = '_blank';
  58. a.download = props.content.name;
  59. a.click();
  60. }
  61. };
  62. </script>
  63. <style lang="scss" scoped>
  64. @import "../../../../assets/styles/common";
  65. .file-message-montainer {
  66. display: flex;
  67. flex-direction: row;
  68. cursor: pointer;
  69. .file-icon {
  70. margin: auto 8px;
  71. }
  72. }
  73. </style>