1

I attempting to download an excel file and check it before sending it on to the user. When I set a breakpoint on var response - oReq.response, I see that oReq.response is null. When I check my network tab, I see that the response is not null rather it is the excel file as expected. Why doesn't the excel file come through to the callback?

var oReq = new XMLHttpRequest();
oReq.open("GET", service, true);
oReq.responseType = "arraybuffer";

oReq.onload = function (oEvent) {
    var response = oReq.response;
    if (response === parseInt(response, 10)) {
        alert("That query had no results");
        $("#dialog").dialog("close");
    }
    else {
        var blob = new Blob([response], { type: "application/vnd.ms-excel" });
        var objectUrl = URL.createObjectURL(blob);
        window.location = objectUrl;
        //saveAs(blob, 'Ad_Hoc_Results.xls');
        $("#dialog").dialog("close");
    }
};
oReq.send();

Response Headers:

Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Disposition:attachment; filename=AdHocReport.xls
Content-Length:6656
Content-Type:application/vnd.ms-excel
Date:Mon, 20 Jul 2015 19:56:50 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
X-SourceFiles:=?UTF-8?B?QzpcRGV2XE5KVEFcTGFuZUNsb3N1cmVBcGlcYXBpXHJlcG9ydFxHZXRBZEhvY1F1ZXJ5UmVwb3J0?=
pQuestions123
  • 4,471
  • 6
  • 28
  • 59
  • 1
    You should be using `oReq.onreadystatechange`, not `oReq.onload`. – Barmar Jul 20 '15 at 19:40
  • 1
    @Barmar onreadystatechange & onload are equivalent http://stackoverflow.com/questions/9181090/is-onload-equal-to-readystate-4-in-xmlhttprequest – 6ton Jul 20 '15 at 19:45
  • 1
    you can't run parseInt() on a whole arraybuffer, just grab the first value if that's what you need. also, response will be an object, which will never "===" compare with the possible outputs of parseInt(). – dandavis Jul 20 '15 at 19:45
  • @dandavis You can, but it will just parse the first integer in the buffer. That code seems to be testing whether the response is an integer instead of a buffer. – Barmar Jul 20 '15 at 19:48
  • When you say you see that the response *is not null*, does that mean it's legal and correct (appropriate headers, status 200, etc...)? – Amit Jul 20 '15 at 19:48
  • 1
    @dandavis I didn't know about that equivalence. Even I learn something new every now and then :) – Barmar Jul 20 '15 at 19:49
  • @dandavis Thanks, yeah, I haven't even gotten to this because I do not get anything but null in the response unless I remove the responseType. Once I remove the responseType I get the resonse with many of the characters converted to question marks.... – pQuestions123 Jul 20 '15 at 19:49
  • @Amit Yes. It has status of 200 and the content is a byte-stream representing the excel file – pQuestions123 Jul 20 '15 at 19:50
  • oh, then check the mime (Content-Type header) of the server response – dandavis Jul 20 '15 at 19:51
  • @dandavis Edited original post and added response headers – pQuestions123 Jul 20 '15 at 19:58
  • 1
    hmmm. nothing jumps out from here. try a blob response instead of an array buffer, since you need a blob anyway. also maybe try taking out the disposition header. – dandavis Jul 20 '15 at 20:07

0 Answers0