问题描述
我正在尝试通过Ajax POST
将Excel文件返回给服务器,服务器生成了.xls文件,但是它没有传递到前端,我认为这与我如何处理响应.
I'm attempting to return an Excel file through a Ajax POST
to the server, the server generates the .xls file, however it doesn't pass through to the front end, i think it has something to do with how i'm handling the response.
它应该启动Excel文件的文件下载.最初我有dataType JSON
,但是在搜索线程后我发现这与dataType
格式和.done
函数有关,但无法弄清楚它应该是什么.
It should kick off a file download of the Excel file. Originally i had dataType JSON
, but after searching through threads i figure it's something to do with the dataType
format and the .done
function, but cannot work out what it should be.
function requestFile(myJSON) {
var urlrequest = "mywebappexport/excel";
var link = $('#exportbtn');
$.ajax({
url: link.attr('href'),
type: "POST",
data: JSON.stringify(myJSON),
cache: true,
contentType: "application/json; charset=utf-8",
complete: function (data) {
var ifr = ($('<iframe />').attr('src', link.attr('href')).hide().appendTo(link))
setTimeout(function () {ifr.remove();}, 5000);
}
})
};
更新的代码:在现代浏览器上可运行,但在IE8上失败-出现错误Unexpected call to method or property access.
在jQuery 1.9.1.js版本中.this.appendChild( elem );
Updated code: working on modern browsers but fails on IE8 - with the errorUnexpected call to method or property access.
In jQuery version 1.9.1.js.this.appendChild( elem );
推荐答案
尝试一下 http://jsfiddle.net/abLeR /
它使用隐藏的iFrame和ajax下载tar文件.相同的文件也可以用于xls文件.
It downloads a tar file using hidden iFrame and ajax. Same can be used for the xls file.
HTML
<a class="download" href="http://ftp.neu.edu.cn/mirrors/eclipse/technology/epp/downloads/release/kepler/SR1/eclipse-java-kepler-SR1-linux-gtk.tar.gz">Download</a>
<span style="display:none;" class="loading">Loading...</span>
JavaScript
$(".download").click(function (e) {
e.preventDefault();
var link = $(this);
$('.loading').show();
$.ajax({
type: 'HEAD',
url: link.attr('href'),
complete: function () {
$('.loading').hide();
var ifr = $('<iframe />').attr('src', link.attr('href')).hide().appendTo(link)
setTimeout(function () {ifr.remove();}, 5000);
}
});
});
这篇关于使用Ajax jQuery POST接收.xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!