视频文件下载

533 阅读1分钟

一般是用a标签下载
有时当a标签的href为.MP4时,它不会下载而是跳转播放
下面代码可以强制进行下载

  download(v) {
    let nameFlag = Date.now() + '.mp4';
    message.info(nameFlag + '已在后台开始下载');
    fetch(v)
      .then(
        (res) => res.blob(),
        () => {
          message.error('下载失败');
        },
      )
      .then(
        (blob) => {
          const a = document.createElement('a');
          document.body.appendChild(a);
          a.style.display = 'none';
          const url = window.URL.createObjectURL(blob);
          a.href = url;
          a.download = nameFlag;
          a.click();
          document.body.removeChild(a);
          window.URL.revokeObjectURL(url);
          debugger;
          message.success(nameFlag + '下载成功');
        },
        (err) => {
          message.error('下载失败');
        },
      );
  }


  download(v) {

let nameFlag = Date.now() + '.mp4';

message.info(nameFlag + '已在后台开始下载');

fetch(v)

.then(

(res) => res.blob(),

() => {

message.error('下载失败');

},

)

.then(

(blob) => {

const a = document.createElement('a');

document.body.appendChild(a);

a.style.display = 'none';

const url = window.URL.createObjectURL(blob);

a.href = url;

a.download = nameFlag;

a.click();

document.body.removeChild(a);

window.URL.revokeObjectURL(url);

debugger;

message.success(nameFlag + '下载成功');

},

(err) => {

message.error('下载失败');

},

);

}