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

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

问题描述

我试图下载使用Ajax(XMLHtt prequest)的Excel文件。

在完成responseText的发现刚刚5个字符。
网络嗅探工具(小提琴手)是表示我,我的计算机接收的整个文件。

那么,为什么这个responseText显示我只有5个字符?我曾经尝试都同步和非同步调用。

感谢您的帮助,您可以在这里得到。

  VAR xmlHtt preQ = getXmlHtt prequestObject();

功能getXmlHtt prequestObject(){
    VAR XMLHTTP;

    如果(window.XMLHtt prequest){// code,对所有新的浏览器
    XMLHTTP =新XMLHtt prequest();

    }否则,如果(window.ActiveXObject){// $ C $下IE5和IE6
    // XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);

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

    对于(i = 0; I< progids.length;我++){
    		尝试 {
    XMLHTTP =新window.ActiveXObject(的ProgID [I]);
    			打破;
    }赶上(五){
    		  //没做什么
    }
    }


    }


    返回XMLHTTP;


}

//为HTTP GET实用方法
功能doSynchronousGet(URL){
    如果(xmlHtt preQ == NULL){
    xmlHtt preQ = getXmlHtt prequestObject();
    }

    //改变最后的参数为true,使异步调用。
    xmlHtt preq.open(GET,网址,虚假);
    xmlHtt preq.setRequestHeader(连接,关闭);
    xmlHtt preq.send(空);
    返回xmlHtt preq.responseText;
}



VAR resultText = doSynchronousGet(URL);

警报('resultText长度:'+ resultText.length);
警报('resultText:'+ resultText);
 

解决方案

这个问题也许在于XMLHtt prequest不后容易采取的二进制数据,如Excel文件。如果你只想让用户下载的文件,阅读拉米兹的职位。如果您需要阅读在JavaScript中的数据,尝试切换到像CSV文本格式(或更好的解析,JSON)。如果你真的需要阅读的二进制文件,还有的是<一个讨论href="http://stackoverflow.com/questions/1095102/how-do-i-load-binary-image-data-using-javascript-and-xmlhtt$p$pquest">here和<一href="http://stackoverflow.com/questions/1919972/how-do-i-access-xhr-responsebody-from-javascript">here.

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

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..

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);

解决方案

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天全站免登陆