Ajax请求下载一个Excel文件显示我截断响应文件、Ajax、Excel

2023-09-10 14:22:25 作者:醉

我试图下载使用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)。如果你真的需要阅读的二进制文件,还有的是here和here.

java使用ajax请求下载excel响应结果显示乱码

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.