Ajax request to download excel file shows me truncated response

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

Upon completion, it was found that responseText has only 5 characters.
The Network Sniffing Tool (Fiddler) shows me that my computer received the entire file.

so why does the responseText answer show me only 5 characters? I tried both Synch and Asynch.

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); 
+3
ajax download
source share
2 answers

Probably the problem is that XMLHttpRequest usually does not use binary data such as an Excel file. If you just want the user to upload the file, read the Ramiz post. If you need to read data in JavaScript, try switching to a text format like CSV (or better for parsing, JSON). If you really need to read the binary, here are discussed here and here .

+1
source share

Do not use the Ajax call (xmlhttprequest) to upload / download files. Better do it server side.

0
source share

All Articles