index.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <PageWrapper title="文件下载示例">
  3. <a-alert message="根据后台接口文件流下载" />
  4. <a-button type="primary" class="my-4" @click="handleDownByData"> 文件流下载 </a-button>
  5. <a-alert message="根据文件地址下载文件" />
  6. <a-button type="primary" class="my-4" @click="handleDownloadByUrl"> 文件地址下载 </a-button>
  7. <a-alert message="base64流下载" />
  8. <a-button type="primary" class="my-4" @click="handleDownloadByBase64"> base64流下载 </a-button>
  9. <a-alert message="图片Url下载,如果有跨域问题,需要处理图片跨域" />
  10. <a-button type="primary" class="my-4" @click="handleDownloadByOnlineUrl"> 图片Url下载 </a-button>
  11. </PageWrapper>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent } from 'vue';
  15. import { downloadByUrl, downloadByData, downloadByBase64, downloadByOnlineUrl } from '/@/utils/file/download';
  16. import imgBase64 from './imgBase64';
  17. import { PageWrapper } from '/@/components/Page';
  18. import { Alert } from 'ant-design-vue';
  19. export default defineComponent({
  20. components: { PageWrapper, [Alert.name]: Alert },
  21. setup() {
  22. function handleDownByData() {
  23. downloadByData('text content', 'testName.txt');
  24. }
  25. function handleDownloadByUrl() {
  26. downloadByUrl({
  27. url: 'https://codeload.github.com/anncwb/vue-Jeecg-admin-doc/zip/master',
  28. target: '_self',
  29. });
  30. downloadByUrl({
  31. url: 'https://vebn.oss-cn-beijing.aliyuncs.com/Jeecg/logo.png',
  32. target: '_self',
  33. });
  34. }
  35. function handleDownloadByBase64() {
  36. downloadByBase64(imgBase64, 'logo.png');
  37. }
  38. function handleDownloadByOnlineUrl() {
  39. downloadByOnlineUrl(
  40. 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5944817f47b8408e9f1442ece49d68ca~tplv-k3u1fbpfcp-watermark.image',
  41. 'logo.png'
  42. );
  43. }
  44. return {
  45. handleDownloadByUrl,
  46. handleDownByData,
  47. handleDownloadByBase64,
  48. handleDownloadByOnlineUrl,
  49. };
  50. },
  51. });
  52. </script>