1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- import { Message } from 'element-ui'
- export default {
- // 新增视频下载方法
- async videoDownload(url, fileName) {
- try {
- // 检查URL是否合法
- if (!url) {
- Message.error('下载地址不能为空');
- return false;
- }
- // 检查文件类型
- const fileType = url.toLowerCase().split('.').pop();
- if (!['mp4', 'mov'].includes(fileType)) {
- Message.error('只支持下载 MP4 或 MOV 格式的文件');
- return false;
- }
- const response = await fetch(url, {
- method: 'GET',
- credentials: 'include', // 包含 cookies
- headers: {
- 'Content-Type': 'application/json',
- }
- });
-
- if (!response.ok) {
- Message.error('下载失败');
- return false;
- }
- const blob = await response.blob();
- const $link = document.createElement('a');
- $link.href = URL.createObjectURL(blob);
- $link.download = fileName || 'video.mp4';
- document.body.appendChild($link);
- $link.click();
- document.body.removeChild($link);
- URL.revokeObjectURL($link.href);
- return true;
- } catch (error) {
- Message.error(error);
- return false;
- }
- }
- }
|