多次點擊下載,列表頁有時會觸發空白頁的情況

列表頁做了一個下載功能,但是多次點擊下載有時會觸發列表頁空白的情況,查詢後找到這樣一個答案:
https://blog.csdn.net/u013009809/article/details/53172675
把下載功能的get請求修改爲post請求就好了,但是通過ajax的post方式提交是調不起來瀏覽器的下載頁的,然後網上檢索,可以通過表單提交的方式發送下載請求
如下

// 原來下載請求
location.href = encodeURI(Hussar.ctxPath + "/excel/download?filePath=" + filePath + "&fileName=" + fileName);
// 修改後下載請求
var downloadURL = Hussar.ctxPath + "/excel/downloadtest";
var form = $("<form>");
form.attr('id','datafrom');
form.attr('style','display:none');
form.attr('target','');
form.attr('method','post');
form.attr('action',downloadURL);
var input  = $('<input>');
input.attr('type','hidden');
input.attr('name','filePath');
input.attr('value',filePath);
form.append(input);
var input1  = $('<input>');
input1.attr('type','hidden');
input1.attr('name','fileName');
input1.attr('value',fileName);
form.append(input1);

$('body').append(form);
form.submit();
$("#datafrom").remove();

測試後多次下載不會出現空白頁的情況了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章