Javascript method to implement downloading: 1. Use the href attribute of the a tag to add the file URL, with the syntax "Download"; 2. Use URL jumps to download, the syntax is "window.open(file url)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer
JS
to achieve downloading File function? The common method is to create a new download:
1. a tag
<a href="前端框架-Minton.rar">下载</a>
function commDownload1(url, params) { url += "?"; for(let key in params) { url += key + "=" + params[key] + "&"; } url = url.substr(0, url.length - 1); $("<a href=" + url + " />")[0].click(); }
2. URL jump
function commDownload2(url, params) { url += "?"; for(let key in params) { url += key + "=" + params[key] + "&"; } url = url.substr(0, url.length - 1); window.open(url);
a tag has a better experience. The other two will open a new tag and download again. The whole process of closing the new tab feels dizzying, so it is recommended to use the
a tag to implement the download function.
The above is the detailed content of How to implement downloading in javascript. For more information, please follow other related articles on the PHP Chinese website!