Download"; 2. Use the url to jump to the download, with the syntax "window. open(file url)"."/> Download"; 2. Use the url to jump to the download, with the syntax "window. open(file url)".">

Home  >  Article  >  Web Front-end  >  How to implement downloading in javascript

How to implement downloading in javascript

奋力向前
奋力向前Original
2021-09-08 09:59:404671browse

Javascript method to implement downloading: 1. Use the href attribute of the a tag to add the file URL, with the syntax "08334b6a1e96957a52fab23a7ec52b14Download5db79b134e9f6b82c0b36e0489ee08ed"; 2. Use URL jumps to download, the syntax is "window.open(file url)".

How to implement downloading in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer

Use JS to achieve downloading File function?

The common method is to create a new download:

1. a tag

##Compressed package download:

<a href="前端框架-Minton.rar">下载</a>

Download other files

 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);

Among them, the

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.

Recommended learning:

JS video tutorial

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!

Statement:
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