Jquery無(wú)須瀏覽實(shí)現(xiàn)直接下載文件
一、常用方式:
1、通常GET方式
后面帶明文參數(shù),不安全。
window.location.href = 'http://localhost:1188/FileDownload.aspx?token=SCBC#';
2、ajax為什么不能下載文件

ajax支持的服務(wù)器返回?cái)?shù)據(jù)類型有:xml、json、script、html,其他類型(例如二進(jìn)制流)將被作為String返回,無(wú)法觸發(fā)瀏覽器的下載處理機(jī)制和程序。
二、可通過(guò)XMLHttpRequest+HTML5 Blob對(duì)象
XMLHttpRequest2 標(biāo)準(zhǔn)支持流文件,使用 xhr.response作為返回(不是responseText)。
jQuery.download_XMLHttpRequest = function (url, fn, data, method) { // 獲得url和data
var xhr = new XMLHttpRequest();
xhr.open(method, url, true);//get請(qǐng)求,請(qǐng)求地址,是否異步
xhr.responseType = "blob"; // 返回類型blob
xhr.onload = function () {// 請(qǐng)求完成處理函數(shù)
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]);
});
// 發(fā)送ajax請(qǐng)求
xhr.send(form);
};調(diào)用:
$.download_XMLHttpRequest('http://localhost:1188/FileDownload.aspx', 'data.jpg', "token=SCBC#", 'post');三、通過(guò)構(gòu)建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); // 把參數(shù)組裝成 form的 input
var inputs = '';
jQuery.each(data.split('&'), function () {
var pair = this.split('=');
inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
}); // request發(fā)送請(qǐng)求
jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove();
};
};調(diào)用:
$.download_Form('http://localhost:1188/FileDownload.aspx', "token=SCBCS#", 'post');或直接:
var url = 'http://localhost:1188/MouldFileDownload.aspx';
// 構(gòu)造隱藏的form表單
var $form = $("<form id='download' class='hidden' method='post'></form>");
$form.attr("action", url);
$(document.body).append($form);
// 添加提交參數(shù)
var $input1 = $("<input name='token' type='hidden'></input>");
$input1.attr("value", "SCBCSAPMould2019~!@#");
$("#download").append($input1);
// 提交表單
$form.submit();到此這篇關(guān)于Jquery無(wú)須瀏覽實(shí)現(xiàn)直接下載文件的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery獲取對(duì)象簡(jiǎn)單實(shí)現(xiàn)方法小結(jié)
jQuery獲取對(duì)象,這里獲取的都是Jquery對(duì)象而不是Dom對(duì)象哦,但是他倆是可以轉(zhuǎn)換滴,新手朋友們可以看看2014-10-10
基于JQuery和CSS3實(shí)現(xiàn)仿Apple TV海報(bào)背景視覺(jué)差特效源碼分享
這是一款效果非常炫酷的jQuery和CSS3仿Apple TV海報(bào)背景視覺(jué)差特效。該視覺(jué)差特效在使用鼠標(biāo)在屏幕上下左右移動(dòng)的時(shí)候,海報(bào)中的各種元素以不同的速度運(yùn)動(dòng),形成視覺(jué)差效果,并且還帶有一些流光特效。2015-09-09
jQuery?獲取與設(shè)置元素屬性的詳細(xì)方法(看完這篇文章就搞明白了)
這篇文章帶領(lǐng)大家熟練掌握?jQuery?的屬性方面的操作,包括固有屬性的獲取與設(shè)置,自定義屬性的獲取與設(shè)置等等,走進(jìn)?jQuery?的更深層次階段,這也是腳本之家小編發(fā)現(xiàn)的一篇比較實(shí)用的文章2023-06-06
jQuery實(shí)現(xiàn)的分頁(yè)插件完整示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的分頁(yè)插件,結(jié)合完整實(shí)例形式分析了jQuery分頁(yè)插件的定義與使用相關(guān)操作技巧,需要的朋友可以參考下2020-05-05
根據(jù)郵箱的域名跳轉(zhuǎn)到相應(yīng)的登錄頁(yè)面的代碼
其實(shí)主要是想記錄一下這種對(duì)象的用法,喜歡的朋友可以參考下2012-02-02
jquery $.ajax()取xml數(shù)據(jù)的小問(wèn)題解決方法
今天想做一個(gè)用$.ajax()從xml中讀取數(shù)據(jù)的這么一個(gè)異步交互過(guò)程2010-11-11
jquery清空textarea等輸入框?qū)崿F(xiàn)代碼
jquery清空textarea等輸入框在工作中很常見(jiàn),接下來(lái)的代碼簡(jiǎn)單實(shí)用,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-04-04

