iframe文件下载

/**
 * 下载文件
 * @param link 文件下载地址
 */
export const download = links => {
    const iframe = document.createElement("iframe");
    iframe.style.display = "none"; // 防止影响页面
    iframe.style.height = 0; // 防止影响页面
    iframe.src = links+'?response-content-type=application/octet-stream';
    document.body.appendChild(iframe); // 这一行必须,iframe挂在到dom树上才会发请求
    // 30s之后删除
    setTimeout(() => {
        iframe.remove();
    }, 30000);
};

       当时使用的这个方案下载文件,后端使用的是阿里云的oss对象存储,然后下载MP4文件的时候一直打开预览,不会触发浏览器下载行为.后面查看阿里云文档,可以设置文件的响应头或者,在请求url后面拼接

'?response-content-type=application/octet-stream';

posted @ 2022-06-30 16:30  静默韶华  阅读(1183)  评论(0编辑  收藏  举报