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ī)制和程序。
二、可通過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');三、通過構(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)文章
Validform驗(yàn)證時(shí)可以為空否則按照指定格式驗(yàn)證
這篇文章主要介紹了Validform驗(yàn)證時(shí)可以為空否則按照指定格式驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2017-10-10
JQuery驗(yàn)證jsp頁(yè)面屬性是否為空(實(shí)例代碼)
JQuery驗(yàn)證jsp頁(yè)面屬性是否為空的實(shí)例代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11
Tab頁(yè)界面 用jQuery及Ajax技術(shù)實(shí)現(xiàn)(php后臺(tái))
到了B/S開發(fā)時(shí)代,網(wǎng)頁(yè)前端布局也把Tab頁(yè)的布局形式吸收了過來。特別是和Ajax技術(shù)結(jié)合起來,可以更充分發(fā)揮Tab頁(yè)的良好表現(xiàn)力和數(shù)據(jù)緩存的優(yōu)勢(shì),是一種良好的網(wǎng)頁(yè)布局形式2011-10-10
jQuery Validate驗(yàn)證框架經(jīng)典大全
本文是腳本之家小編給大家分享的jquery validate驗(yàn)證框架大全,本文內(nèi)容講的非常詳細(xì),感興趣的朋友一起看看吧2015-09-09
JQuery實(shí)現(xiàn)動(dòng)態(tài)操作表格
本文主要分享了jQuery實(shí)現(xiàn)對(duì)一個(gè)表格動(dòng)態(tài)的添加行,刪除行,并且對(duì)表格中內(nèi)容進(jìn)行非空驗(yàn)證的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
利用JQuery寫一個(gè)簡(jiǎn)單的異步分頁(yè)插件
這篇文章主要為大家詳細(xì)介紹了如何利用JQuery寫一個(gè)簡(jiǎn)單的異步分頁(yè)插件,感興趣的小伙伴們可以參考一下2016-03-03
Jquery+ajax請(qǐng)求data顯示在GridView上(asp.net)
Jquery ajax請(qǐng)求data顯示在asp.net中GridView控件上,需要的朋友可以參考下。2010-08-08
jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法
這篇文章主要介紹了jQuery選擇id屬性帶有點(diǎn)符號(hào)元素的方法,涉及jQuery選擇器的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
jQuery實(shí)現(xiàn)提交表單時(shí)不提交隱藏div中input的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)提交表單時(shí)不提交隱藏div中input的方法,結(jié)合實(shí)例形式分析了通過設(shè)置input的disabled屬性實(shí)現(xiàn)隱藏input提交的操作技巧,需要的朋友可以參考下2019-10-10
jQuery實(shí)現(xiàn)每日秒殺商品倒計(jì)時(shí)功能
這篇文章主要介紹了 jQuery實(shí)現(xiàn)每日秒殺商品倒計(jì)時(shí)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

