Home > Web Front-end > JS Tutorial > body text

How to implement file download function in JavaScript

不言
Release: 2018-07-14 15:57:40
Original
7025 people have browsed it

This article mainly introduces how JavaScript implements the file download function. It has a certain reference value. Now I share it with you. Friends in need can refer to

1.H5 download attribute

function downFile(content, filename) {    // 创建隐藏的可下载链接
    var eleLink = document.createElement('a');
    eleLink.download = filename;
    eleLink.style.display = 'none';    // 字符内容转变成blob地址
    var blob = new Blob([content]);
    eleLink.href = URL.createObjectURL(blob);    // 触发点击    document.body.appendChild(eleLink);
    eleLink.click();    // 然后移除    document.body.removeChild(eleLink);
};
Copy after login

2.iframe method

// if (typeof(download.iframe) == 'undefined') {
            //     var iframe = document.createElement('iframe');
            //     download.iframe = iframe;
            //     document.body.appendChild(download.iframe);
            // };
            // download.iframe.src = newdownloadUrl;
            // download.iframe.style.display = "none";
Copy after login

3.form method

// var $eleForm = $("<form method=&#39;get&#39;></form>");
            // $eleForm.attr("action", "https://codeload.github.com/douban/douban-client/legacy.zip/master");
            // $eleForm.attr("action", url);
            // $(document.body).append($eleForm);
            // $eleForm.submit();
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use Element form validation in vue

How to use the JavaScript console to improve the work process

The above is the detailed content of How to implement file download function in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!