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); };
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";
3.form method
// var $eleForm = $("<form method='get'></form>"); // $eleForm.attr("action", "https://codeload.github.com/douban/douban-client/legacy.zip/master"); // $eleForm.attr("action", url); // $(document.body).append($eleForm); // $eleForm.submit();
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!