Jquery无须浏览实现直接下载文件
一、常用方式:
1、通常GET方式
后面带明文参数,不安全。
window.location.href = 'http://localhost:1188/FileDownload.aspx?token=SCBC#’;
2、ajax为什么不能下载文件

ajax支持的服务器返回数据类型有:xml、json、script、html,其他类型(例如二进制流)将被作为String返回,无法触发浏览器的下载处理机制和程序。
二、可通过XMLHttpRequest+HTML5 Blob对象
XMLHttpRequest2 标准支持流文件,使用 xhr.response作为返回(不是responseText)。
jQuery.download_XMLHttpRequest = function (url, fn, data, method) { // 获得url和data
    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);//get请求,请求地址,是否异步
    xhr.responseType = "blob";    // 返回类型blob
    xhr.onload = function () {// 请求完成处理函数
        if (this.status === 200) {
            var blob = this.response;// 获取返回值
            if (navigator.msSaveBlob) // IE10 can't do a[download], only Blobs:
            {
                window.navigator.msSaveBlob(blob, fn);
                return;
            }
            if (window.URL) { // simple fast and modern way using Blob and URL:
                var a = document.createElement('a');
                var oURL = window.URL.createObjectURL(blob);
                if ('download' in a) { //html5 A[download]
                    a.href = oURL;
                    a.setAttribute("download", fn);
                    a.innerHTML = "downloading...";
                    document.body.appendChild(a);
                    setTimeout(function () {
                        a.click();
                        document.body.removeChild(a);
                        setTimeout(function () {
                            window.URL.revokeObjectURL(a.href);
                        }, 250);
                    }, 66);
                    return;
                }
                //do iframe dataURL download (old ch+FF):
                var f = document.createElement("iframe");
                document.body.appendChild(f);
                oURL = "data:" + oURL.replace(/^data:([\w\/\-\+]+)/, "application/octet-stream");
                f.src = oURL;
                setTimeout(function () {
                    document.body.removeChild(f);
                }, 333);
            }
        }
    };
    var form = new FormData();
    jQuery.each(data.split('&'), function () {
        var pair = this.split('=');
        form.append(pair[0], pair[1]);
    });
    // 发送ajax请求
    xhr.send(form);
};
调用:
$.download_XMLHttpRequest('http://localhost:1188/FileDownload.aspx', 'data.jpg', "token=SCBC#", 'post');
三、通过构建Form表单提交
jQuery.download_Form = function (url, data, method) { // 获得url和data
    if (url && data) {
        // data 是 string 或者 array/object
        data = typeof data == 'string' ? data : jQuery.param(data); // 把参数组装成 form的 input
        var inputs = '';
        jQuery.each(data.split('&'), function () {
            var pair = this.split('=');
            inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
        }); // request发送请求
        jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove();
    };
};
调用:
$.download_Form('http://localhost:1188/FileDownload.aspx', "token=SCBCS#", 'post');
或直接:
var url = 'http://localhost:1188/MouldFileDownload.aspx';
// 构造隐藏的form表单
var $form = $("<form id='download' class='hidden' method='post'></form>");
$form.attr("action", url);
$(document.body).append($form);
// 添加提交参数
var $input1 = $("<input name='token' type='hidden'></input>");
$input1.attr("value", "SCBCSAPMould2019~!@#");
$("#download").append($input1); // 提交表单
$form.submit();
Jquery无须浏览实现直接下载文件的更多相关文章
- Linux中7个用来浏览网页和下载文件的命令
		
上一篇文章中,我们提到了rTorrent.wget.cURL.w3m.Elinks等几个有用的工具,很多人回信说还有其它几个类似的工具也值得讨论,所以就有了这篇文章.如果错过了第一部分的讨论,可以通过 ...
 - 怎样用idhttpserver代替IIS让用户浏览html或下载文件   http://bbs.csdn.net/topics/360248674
		
怎样用idhttpserver代替IIS让用户浏览html或下载文件 更多0分享到: 相关知识库: C# 虚拟现实(VR) Node.js 算法与数据结构 对我有用[0] 丢个板砖[0] ...
 - jquery.form.js ie 下下载文件已经ie8失效问题解决方案
		
https://github.com/malsup/form/blob/master/jquery.form.js在使用这个插件时遇到的问题1.ie下会变成下载文件,解决方案是在后端返回时设置'Con ...
 - JS JQuery 操作: Json转 Excel 下载文件
		
方法的调用 var json = '[' + '{"申请流水号":"123456","保险公司":"测试数据",&quo ...
 - 解决FileZilla访问手机ftp服务只能删除浏览文件不能下载文件的问题
		
用了Linux系统之后,很多资源都不方便获取, 因为很多资料都是放在百某某盘上面. 无意中看到我手机有FTP服务,想到我电脑装有FileZilla,可以访问手机了 然后就连接上了. 但是浏览的时候发现 ...
 - jQuery用FormData对象实现文件上传以及如何通过ajax下载文件
		
之前在Vue的项目里面用到过文件上传,封装好的组件用起来比较顺手,查询Element-UI文档,十八般武器样样都有,一顿操作猛如虎,一看--跑偏了(⊙o⊙)-,我的意思就是用框架实现比较简单,但是如果 ...
 - Nginx 配置浏览Linux 系统目录并下载文件
		
准备工作: 安装编译工具及库文件: yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 安装PCRE( ...
 - jQuery --- 利用a标签的download属性下载文件!
		
最近遇到一个项目,需要有点击下载文件的功能. 由于文件格式是多种的,对于 rar / zip / rtf / doc / xlsx / jpg等. 点击下载有的是直接跳转到后进行下载,但有的是打开进行 ...
 - 使用js实现点击按钮下载文件
		
有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...
 
随机推荐
- 用PowerDesigner将SQL转pdm文件
			
1.打开新建的pdm文件,File->Reverse Enginner->Databases 2.选择对应数据库(DBMS),点击确定,然后添加SQL文件 3.点击确定 注:如果DBMS没 ...
 - kafka备份原理
 - Java Web报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
			
问题描述: 我们在用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not foun ...
 - APIO2019题解
			
T1.桥梁(bridges/restriction) Subtask1:暴力,$O(n^2)$. #include<cstdio> #include<algorithm> #d ...
 - Bash速查表
			
例 #!/usr/bin/env bash NAME="John" echo "Hello $NAME!" 变量 NAME="John" e ...
 - C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思?
			
原文:C#/.NET 中启动进程时所使用的 UseShellExecute 设置为 true 和 false 分别代表什么意思? 在 .NET 中创建进程时,可以传入 ProcessStartInfo ...
 - 【C#】 获取计算机的硬件信息
			
添加引用:System.Management /// <summary> /// 获取本机的MAC地址 /// </summary> /// <returns>&l ...
 - js调用浏览器下载
			
$scope.Download = function (url) { var save_link = document.createElementNS("http://www.w3.org/ ...
 - MySQL语法顺序及执行顺序
			
一.书写顺序 select[distinct] from join on where group by having union order by limit 二.执行顺序 from on join ...
 - Django 信号使用问题
			
Django 信号使用问题: 在使用django内置信号修改新注册的用户密码的时候,发现内置信号没有被触发.百度&官方文档找到了答案 1.信号的函数应该放在哪里? 这段代码应该放在哪里? 严格 ...