index.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div class="m-5 demo-box">
  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">
  11. 图片Url下载
  12. </a-button>
  13. </div>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent } from 'vue';
  17. import {
  18. downloadByUrl,
  19. downloadByData,
  20. downloadByBase64,
  21. downloadByOnlineUrl,
  22. } from '/@/utils/file/download';
  23. import imgBase64 from './imgBase64';
  24. export default defineComponent({
  25. setup() {
  26. function handleDownByData() {
  27. downloadByData('text content', 'testName.txt');
  28. }
  29. function handleDownloadByUrl() {
  30. downloadByUrl({
  31. url: 'https://codeload.github.com/anncwb/vue-vben-admin-doc/zip/master',
  32. target: '_self',
  33. });
  34. downloadByUrl({
  35. url: 'https://vebn.oss-cn-beijing.aliyuncs.com/vben/logo.png',
  36. target: '_self',
  37. });
  38. }
  39. function handleDownloadByBase64() {
  40. downloadByBase64(imgBase64, 'logo.png');
  41. }
  42. function handleDownloadByOnlineUrl() {
  43. downloadByOnlineUrl(
  44. 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5944817f47b8408e9f1442ece49d68ca~tplv-k3u1fbpfcp-watermark.image',
  45. 'logo.png'
  46. );
  47. }
  48. return {
  49. handleDownloadByUrl,
  50. handleDownByData,
  51. handleDownloadByBase64,
  52. handleDownloadByOnlineUrl,
  53. };
  54. },
  55. });
  56. </script>