使用Ajax jQuery POST接收.xls文件 [英] Receiving a .xls file using Ajax jquery POST

查看:724
本文介绍了使用Ajax jQuery POST接收.xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过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 error Unexpected 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆