Home > Backend Development > PHP Tutorial > js怎么实现点击下载文件?

js怎么实现点击下载文件?

PHPz
Release: 2020-09-04 17:28:29
Original
2168 people have browsed it

js怎么实现点击下载文件?

js实现点击下载文件的方法:

实现方法

var link = document.createElement('a');
link.setAttribute("download", "");
link.href = "you file url";
link.click();
Copy after login

解释:创建一个a标签,然后设置 download 属性,这个是重点,有这个属性才能下载,然后设置下载的链接。

在网上找了很多的方法,基本就是通过下面方法打开:

window.location.href="你的url"; 
 
window.open("你的url")
Copy after login

但是文件是 txt 或者 img 的话,那么谷歌浏览器就会直接打开这些文件。

更多相关技术文章,请访问PHP中文网

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