Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

69.前端如何实现文件下载 #69

Open
wqjiao opened this issue Sep 11, 2020 · 0 comments
Open

69.前端如何实现文件下载 #69

wqjiao opened this issue Sep 11, 2020 · 0 comments

Comments

@wqjiao
Copy link
Owner

wqjiao commented Sep 11, 2020

  • 在线图片/文件下载

类似接口请求方式下载文件:

/**
 * 使用 fetch/ajax 下载在线图片/文件
 * @param {*} url 文件地址 
 * @param {*} filename 文件名称
 */
function download(url, filename) {
    return fetch(url).then(res => res.blob().then(blob => {
        let a = document.createElement('a');
        let url = window.URL.createObjectURL(blob);
        a.href = url;
        a.download = filename;
        a.click();
        window.URL.revokeObjectURL(url);
    }))
}
  • a标签下载

下载 base64、文件流等

/**
 * @param {*} fileBlob 文件流
 * @param {*} filename 文件名称
 * @description 导出下载订单
 */
function exportDownload(fileBlob, filename) {
    let elink = document.createElement('a');

    elink.download = filename;
    elink.href = fileBlob;
    elink.click();
}
@wqjiao wqjiao changed the title 69.前端如何实现图片下载 69.前端如何实现文件下载 Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant