index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <script setup lang="ts">
  2. import { ref } from 'vue';
  3. import { Page } from '@vben/common-ui';
  4. import {
  5. downloadFileFromBase64,
  6. downloadFileFromBlobPart,
  7. downloadFileFromImageUrl,
  8. downloadFileFromUrl,
  9. } from '@vben/utils';
  10. import { Button, Card } from 'antdv-next';
  11. import { downloadFile1, downloadFile2 } from '#/api/examples/download';
  12. import imageBase64 from './base64';
  13. const downloadResult = ref('');
  14. function getBlob() {
  15. downloadFile1().then((res) => {
  16. downloadResult.value = `获取Blob成功,长度:${res.size}`;
  17. });
  18. }
  19. function getResponse() {
  20. downloadFile2().then((res) => {
  21. downloadResult.value = `获取Response成功,headers:${JSON.stringify(res.headers)},长度:${res.data.size}`;
  22. });
  23. }
  24. </script>
  25. <template>
  26. <Page title="文件下载示例">
  27. <Card title="根据文件地址下载文件">
  28. <Button
  29. type="primary"
  30. @click="
  31. downloadFileFromUrl({
  32. source:
  33. 'https://codeload.github.com/vbenjs/vue-vben-admin-doc/zip/main',
  34. target: '_self',
  35. })
  36. "
  37. >
  38. Download File
  39. </Button>
  40. </Card>
  41. <Card class="my-5" title="根据地址下载图片">
  42. <Button
  43. type="primary"
  44. @click="
  45. downloadFileFromImageUrl({
  46. source:
  47. 'https://unpkg.com/@vbenjs/static-source@0.1.7/source/logo-v1.webp',
  48. fileName: 'vben-logo.png',
  49. })
  50. "
  51. >
  52. Download File
  53. </Button>
  54. </Card>
  55. <Card class="my-5" title="base64流下载">
  56. <Button
  57. type="primary"
  58. @click="
  59. downloadFileFromBase64({
  60. source: imageBase64,
  61. fileName: 'image.png',
  62. })
  63. "
  64. >
  65. Download Image
  66. </Button>
  67. </Card>
  68. <Card class="my-5" title="文本下载">
  69. <Button
  70. type="primary"
  71. @click="
  72. downloadFileFromBlobPart({
  73. source: 'text content',
  74. fileName: 'test.txt',
  75. })
  76. "
  77. >
  78. Download TxT
  79. </Button>
  80. </Card>
  81. <Card class="my-5" title="Request download">
  82. <Button type="primary" @click="getBlob"> 获取Blob </Button>
  83. <Button type="primary" class="ml-4" @click="getResponse">
  84. 获取Response
  85. </Button>
  86. <div class="mt-4">{{ downloadResult }}</div>
  87. </Card>
  88. </Page>
  89. </template>