Js点击按钮下载文件到本地(兼容多浏览器)
实现点击 用纯 js(非jquery) 下载文件到本地
自己尝试,加网上找了好久未果,如:
window.open(url) location.href=url form表单提交 iframe 体验和浏览器兼容都不完美
还是博客园一兄弟给了方法,非常感谢!
window.downloadFile = function (sUrl) {
//iOS devices do not support downloading. We have to inform user about this.
if (/(iP)/g.test(navigator.userAgent)) {
alert('Your device does not support files downloading. Please try again in desktop browser.');
return false;
}
//If in Chrome or Safari - download via virtual link click
if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
//Creating new link node.
var link = document.createElement('a');
link.href = sUrl;
if (link.download !== undefined) {
//Set HTML5 download attribute. This will prevent file from opening if supported.
var fileName = sUrl.substring(sUrl.lastIndexOf('/') + , sUrl.length);
link.download = fileName;
}
//Dispatching click event.
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initEvent('click', true, true);
link.dispatchEvent(e);
return true;
}
}
// Force file download (whether supported by server).
if (sUrl.indexOf('?') === -) {
sUrl += '?download';
}
window.open(sUrl, '_self');
return true;
}
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -;
Js点击按钮下载文件到本地(兼容多浏览器)的更多相关文章
- JS点击按钮下载文件
通过form表单提交: 由于ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以通过ajax去请求该接口是无法下载文件的,所以我们创建一个新的form元素来请求接 ...
- 使用js实现点击按钮下载文件
有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...
- 使用JS代码实现点击按钮下载文件
有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...
- js点击按钮保存数据到本地
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- js 点击按钮下载图片,另存为
js: 1 $(document).on('click',"#xiazai",function(){ 2 imgurl = $(".img-box").find ...
- 页面点击按钮下载excel(原生js)
let els = document.getElementsByTagName('iframe'); if(els.length > 0){ for(let i = 0;i < els.l ...
- Java 从服务器下载文件到本地(页面、后台、配置都有)
先来看实现效果: 有一个链接如下: 点击链接下载文件: 第一种方法:Servlet实现 一.HTML页面部分: 1.HTML页面中的一个链接 <a id="downloadTempl ...
- PHP实现远程下载文件到本地
PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载 经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经 ...
- PHP CURL实现远程下载文件到本地
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...
随机推荐
- python3 hashlib模块
hashlib是一个加密模块,可以将明文加密为密文: md5,sha加密:过程不可逆转.
- 20165223 week2测试补交与总结
测试题二 题目: 在Ubuntu或Windows命令行中 建如下目录结构 Hello.java的内容见附件package isxxxx; (xxxx替换为你的四位学号) 编译运行Hello.java ...
- Jupyter-Notebook服务器自定义密码
往期回顾 Anaconda安装:https://www.cnblogs.com/dotnetcrazy/p/9158715.html 基本知识导航篇:https://www.cnblogs.com/d ...
- GNOME 3.28 启用桌面图标
原由 GNOME 3.28合并了移除桌面图标支持 Nautilus从此不在支持桌面图标,直到找到新的解决方案 Issues地址:https://gitlab.gnome.org/GNOME/nauti ...
- Ubuntu 16.04交换Ctrl和Caps
将Caps这个鸡肋的键位换成Ctrl的人不在少数,Ubuntu 12.04 中可以通过设置-键盘更改,新版去掉了这个功能,可以通过修改系统文件实现 方法1 在~/.xinputrc中加入:setxkb ...
- 纪中2018暑假培训day5提高b组改题记录
因为今天省选组也做a组,以为今天a组会很难,就做了做b组.t1和t3强行暴力,好在有t2保底.t1和正解就差一点,然而考试时死活想不起来...... 今天改题可以少改一道了!ovo 救救孩子吧!t1T ...
- Autotools知识点
最近研究了下glog使用autotools编译方法的脚本文件,略有所得 configure.ac AC_INIT初始化一些信息 Package Version ReportBug AC_CONFIG_ ...
- Vuex速学篇:(2)利用state保存新闻数据
回顾 以前我们在做这个新闻列表的时候,是一个写死的数据 export default{ data(){ return{ newslist:[ {newsid:"101",pubti ...
- mysql中using
select * from ( SELECT u.utm_source ,count(DISTINCT u.mobile) as new_user -- 登记用户 FROM 表名 u WHERE u. ...
- Emgu.CV 播放视频-本地文件/RTSP流
using Emgu.CV; using System; using System.Drawing; using System.Threading; using System.Windows.Form ...