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

js 下载文件 #37

Open
tjlizz opened this issue Dec 15, 2020 · 0 comments
Open

js 下载文件 #37

tjlizz opened this issue Dec 15, 2020 · 0 comments

Comments

@tjlizz
Copy link
Owner

tjlizz commented Dec 15, 2020

 var url = 'download/?filename=aaa.txt';
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);      
    xhr.responseType = "blob";    
    xhr.onload = function () {
        if (this.status === 200) {
            var blob = this.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob);     
            reader.onload = function (e) {
                var a = document.createElement('a');
                a.download = 'name';
                a.href = e.target.result;
                $("body").append(a);   
                a.click();
                $(a).remove();
            }
        }
    };
    xhr.send()
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