downloadUtil.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Message } from 'element-ui'
  2. export default {
  3. // 新增视频下载方法
  4. async videoDownload(url, fileName) {
  5. try {
  6. // 检查URL是否合法
  7. if (!url) {
  8. Message.error('下载地址不能为空');
  9. return false;
  10. }
  11. // 检查文件类型
  12. const fileType = url.toLowerCase().split('.').pop();
  13. if (!['mp4', 'mov'].includes(fileType)) {
  14. Message.error('只支持下载 MP4 或 MOV 格式的文件');
  15. return false;
  16. }
  17. const response = await fetch(url, {
  18. method: 'GET',
  19. credentials: 'include', // 包含 cookies
  20. headers: {
  21. 'Content-Type': 'application/json',
  22. }
  23. });
  24. if (!response.ok) {
  25. Message.error('下载失败');
  26. return false;
  27. }
  28. const blob = await response.blob();
  29. const $link = document.createElement('a');
  30. $link.href = URL.createObjectURL(blob);
  31. $link.download = fileName || 'video.mp4';
  32. document.body.appendChild($link);
  33. $link.click();
  34. document.body.removeChild($link);
  35. URL.revokeObjectURL($link.href);
  36. return true;
  37. } catch (error) {
  38. Message.error(error);
  39. return false;
  40. }
  41. }
  42. }