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

ajax支持的服務(wù)器返回數(shù)據(jù)類型有:xml、json、script、html,其他類型(例如二進制流)將被作為String返回,無法觸發(fā)瀏覽器的下載處理機制和程序。
二、可通過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 () {// 請求完成處理函數(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請求
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ā)送請求
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無須瀏覽實現(xiàn)直接下載文件的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于JQuery和CSS3實現(xiàn)仿Apple TV海報背景視覺差特效源碼分享
這是一款效果非常炫酷的jQuery和CSS3仿Apple TV海報背景視覺差特效。該視覺差特效在使用鼠標在屏幕上下左右移動的時候,海報中的各種元素以不同的速度運動,形成視覺差效果,并且還帶有一些流光特效。2015-09-09
jQuery?獲取與設(shè)置元素屬性的詳細方法(看完這篇文章就搞明白了)
這篇文章帶領(lǐng)大家熟練掌握?jQuery?的屬性方面的操作,包括固有屬性的獲取與設(shè)置,自定義屬性的獲取與設(shè)置等等,走進?jQuery?的更深層次階段,這也是腳本之家小編發(fā)現(xiàn)的一篇比較實用的文章2023-06-06
根據(jù)郵箱的域名跳轉(zhuǎn)到相應(yīng)的登錄頁面的代碼
其實主要是想記錄一下這種對象的用法,喜歡的朋友可以參考下2012-02-02
jquery $.ajax()取xml數(shù)據(jù)的小問題解決方法
今天想做一個用$.ajax()從xml中讀取數(shù)據(jù)的這么一個異步交互過程2010-11-11
jquery清空textarea等輸入框?qū)崿F(xiàn)代碼
jquery清空textarea等輸入框在工作中很常見,接下來的代碼簡單實用,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04

