vue+js 下载本地文件

第一步

先把要下载的模板放在public目录下

第二步

使用如下方法download()进行下载,其中url前加上`${process.env.BASE_URL}`,例如`${process.env.BASE_URL}fileResources/template.xlsx`,

function download (url, filename) {
    const anchor = document.createElement("a");
    anchor.href = url;
    anchor.setAttribute("download", filename);
    anchor.innerHTML = "downloading...";
    anchor.style.display = "none";
    document.body.appendChild(anchor);
    setTimeout(() => {
        anchor.click();
        document.body.removeChild(anchor);
        setTimeout(() => { self.URL.revokeObjectURL(anchor.href); }, 250);
    }, 66);
}

猜你喜欢

转载自blog.csdn.net/baidu_38798835/article/details/111831798