ajax请求下载一个excel文件显示我截断的响应 [英] ajax request to download an excel file is showing me truncated response

查看:273
本文介绍了ajax请求下载一个excel文件显示我截断的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax(XMLHttpRequest)下载一个Excel文件。

I am trying to download an Excel file using Ajax (XMLHttpRequest).

完成后,responseText被发现只有5个字符。

网络嗅探工具(Fiddler)显示我的电脑收到了整个文件..

On completion the responseText is found to have just 5 characters.
The network sniffing tool (Fiddler) is showing me that my computer received the entire file..

所以为什么responseText只显示5个字符?我已经尝试了Synch和Asynch电话。

so why is the responseText showing me only 5 characters? I have tried both Synch and Asynch calls.

感谢您可以在这里提供任何帮助。

Thanks for any help you can give here.

var xmlHttpReq = getXmlHttpRequestObject();

function  getXmlHttpRequestObject(){
    var xmlhttp;

    if (window.XMLHttpRequest){// code for all new browsers
          xmlhttp=new XMLHttpRequest();

    }else if (window.ActiveXObject){// code for IE5 and IE6
         // xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

        progids = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'Microsoft.XMLHTTP'];

        for (i=0 ; i < progids.length; i ++){
            try {
                xmlhttp = new window.ActiveXObject(progids[i]);
                break;
            } catch (e) {
              //do nothing
            }
        }


    }


    return xmlhttp;


}

//utility method for http get
function doSynchronousGet(url){
    if(xmlHttpReq == null){
        xmlHttpReq = getXmlHttpRequestObject();
    }

    //change last param to true for making async calls.
    xmlHttpReq.open("GET" ,url,false);
    xmlHttpReq.setRequestHeader("Connection", "close");
    xmlHttpReq.send(null);
    return xmlHttpReq.responseText;
}



var resultText = doSynchronousGet(url);

alert('resultText length: '+ resultText.length);
alert('resultText: '+ resultText);


推荐答案

问题可能是XMLHttpRequest没有通过二进制数据,如Excel文件。如果您只想让用户下载该文件,请阅读Ramiz的文章。如果您需要阅读JavaScript中的数据,请尝试切换为CSV格式的文本格式(或更好的解析JSON)。如果您真的需要阅读二进制文件,那么就会讨论此处此处

The issue is probably that XMLHttpRequest doesn't ususally take binary data like an Excel file. If you just want to let the user download the file, read Ramiz's post. If you need to read the data in JavaScript, try switching to a text format like CSV (or better for parsing, JSON). If you really need to read a binary file, there are discussions of that here and here.

这篇关于ajax请求下载一个excel文件显示我截断的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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