index.vue 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <template>
  2. <div>
  3. <el-button type="primary" @click="download">下载</el-button>
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import { defineComponent, ref } from 'vue';
  8. import { ElButton, ElMessage } from 'element-plus';
  9. import axios from 'axios';
  10. export default defineComponent({
  11. components: { ElButton },
  12. props: {
  13. urls: { type: Array, required: true },
  14. fileName: { type: String, default: '' },
  15. },
  16. setup(props) {
  17. const urls = ref(props.urls);
  18. const download = () => {
  19. if (!urls.value.length) {
  20. ElMessage.error('没有可下载的文件');
  21. return;
  22. }
  23. const anchor = document.createElement('a');
  24. anchor.style.display = 'none';
  25. anchor.href = '#';
  26. document.body.appendChild(anchor);
  27. const formData = new FormData();
  28. urls.value.forEach(el => {
  29. axios
  30. .get(el.video_url, {
  31. responseType: 'blob',
  32. })
  33. .then((response) => {
  34. anchor.download = `《${props.fileName}》${el.series_name}.mp4`;
  35. const blob = new Blob([response.data]);
  36. const objectUrl = URL.createObjectURL(blob);
  37. anchor.href = objectUrl;
  38. anchor.click();
  39. URL.revokeObjectURL(objectUrl);
  40. })
  41. .catch((error) => {
  42. console.error(error);
  43. ElMessage.error('下载失败');
  44. });
  45. })
  46. };
  47. return { download };
  48. },
  49. });
  50. </script>