js經過 URL下載文件

頁面上一個button,點擊以後觸發一個function去請求數據,返回 pdf/epub 的URL,而後下載這個文件.html

原本是直接用 a 寫的,href裏放資源地址,target設爲'_blank'效果良好.但這樣全部的資源都會直接暴露並且不方便統計,因此就換用上面的方式.web

  1. js構造a標籤,js觸發click.不加_blank是能夠的,但會替換掉原來整個頁面.加了_blank chrome就攔截,Safari無效果.ajax

  2. 用iframe,src填入資源地址. 但這樣Safari無效果. 且,若是是MP4/pdf這樣的文件瀏覽器並無下載而是直接打開.chrome

  3. window.open 被攔截json

最想要的效果是點擊下載按鈕,原頁面無改動無刷新,直接下載資源.移動版在新頁面下載,觸發下載自動關閉新頁面.瀏覽器

解決辦法:安全

兩種方案:app

一 ,使用第一種方式,js構造a標籤,js觸發click.在a標籤中加入download屬性,(360安全瀏覽器兼容模式和IE瀏覽器可能仍是會彈出空白頁)jsp

jsp頁面:函數

1 <div class="controls chzn-select fn-left">
2     <button class="button button-flat-primary button-rounded"     type="button" id="export-self-static">導出
3     </button>
4 </div>
5 
6 <a id="exportInfoForm" style="display: none;" download><li id="ex-li">公式管理</li></a>

js頁面:

1 $('#export-static').unbind('click').bind('click',function(){
2 exports.exportSelfStatistics();
3 });
4 
5 var url = contextPath+"statistics/self/detail/export?" + new Date().getTime()+param;
6 $('#exportInfoForm').attr("href", Util.appEncodeURL(url));
7 $('#ex-li').trigger("click");

 二,JQuery的ajax函數的返回類型只有xml、text、json、html等類型,沒有「流」類型,因此咱們要實現ajax下載,不可以使用相應的ajax函數進行文件下載。

但能夠用js生成一個form,用這個form提交參數,並返回「流」類型的數據。在實現過程當中,頁面也沒有進行刷新.(由於是動態生成的表單,靜態的仍是會刷新,要使用form.ajaxsubmit)

1)get請求

 1 $('.download').click(function () {
 2 var tt = new Date().getTime();
 3 var url = 'http://192.168.1.231:8080/91survey/ws/excel/download';
 4 /**
 5 * 使用form表單來發送請求
 6 * 1.method屬性用來設置請求的類型——post仍是get
 7 * 2.action屬性用來設置請求路徑。
 8 * 
 9 */
10 var form=$("<form>");//定義一個form表單
11 form.attr("style","display:none");
12 form.attr("target","");
13 form.attr("method","get"); //請求類型
14 form.attr("action",url); //請求地址
15 $("body").append(form);//將表單放置在web中
16   /**
17 * input標籤主要用來傳遞請求所需的參數:
18 *
19 * 1.name屬性是傳遞請求所需的參數名.
20 * 2.value屬性是傳遞請求所需的參數值.
21 *
22 * 3.當爲get類型時,請求所需的參數用input標籤來傳遞,直接寫在URL後面是無效的。
23 * 4.當爲post類型時,queryString參數直接寫在URL後面,formData參數則用input標籤傳遞
24 * 有多少數據則使用多少input標籤
25 *
26    */
27 var input1=$("<input>");
28 input1.attr("type","hidden");
29 input1.attr("name","tt");
30 input1.attr("value",tt);
31 form.append(input1);
32 var input2=$("<input>");
33 input2.attr("type","hidden");
34 input2.attr("name","companyId");
35 input2.attr("value",companyId);
36 form.append(input2);
37 form.submit();//表單提交
38 })

2)post請求

 1 $('.download').click(function(){
 2 var tt =newDate().getTime();
 3 var url = restUrl +'/excel/download?userId='+ userId;
 4 var form=$("<form>");//定義一個form表單
 5 form.attr("style","display:none");
 6 form.attr("target","");
 7 form.attr("method","post");//請求類型
 8 form.attr("action",url);//請求地址
 9 $("body").append(form);//將表單放置在web中
10 var input1=$("<input>");
11 input1.attr("type","hidden");
12 input1.attr("name","tt");
13 input1.attr("value",tt);
14 form.append(input1);
15 var input2=$("<input>");
16 input2.attr("type","hidden");
17 input2.attr("name","companyId");
18 input2.attr("value",companyId);
19 form.append(input2);
20 form.submit();//表單提交
21 });

完成後,在頁面上面點擊下載圖標,文件就會自動下載了。

相關文章
相關標籤/搜索