0

How can i download XSLX or DOCX file using XMLHTTPRequest response in IE 9 ? I only need the IE 9 Solution.

This code works good for plain-text and csv Files but when i try to download XSLX or DOCX files nothing happens.

The response came from a java servlet with response-content-type set to

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

The javascript function that i need for downloading files.

function submitForm(command, event) {
            disableButtons();
            var form = $('form[name=myForm]');
            var dataString = form.serialize();
            var xhr = new XMLHttpRequest();
            xhr.open('POST', myURL, true);
            //tested => xhr.responseType = 'arraybuffer';
            //tested => try { xhr.responseType = "arraybuffer"; } catch (e) { }; application/zip
            //tested => try { xhr.responseType = "msxml-document"; } catch (e) { };

            xhr.onreadystatechange = function() {

                if(xhr.readyState == 4){
                    // xhr is complete (200 for web servers, 0 for local files in IE)
                    if ((xhr.status == 200)||(xhr.status == 0)){
                        // good

                        var filename = "";
                        var disposition = xhr.getResponseHeader('Content-Disposition');
                        if (disposition && disposition.indexOf('attachment') !== -1) {
                            var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
                            var matches = filenameRegex.exec(disposition);
                            if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
                        }
                        var type = xhr.getResponseHeader('Content-Type');
                        var URL = window.URL || window.webkitURL;
                        var downloadUrl = URL.createObjectURL(blob);

                        var frame = document.createElement('iframe');
                        document.body.appendChild(frame);

                        frame.contentWindow.document.open(type, "replace");
                        frame.contentWindow.document.write(xhr.responseText); //tried also responseXML
                        frame.contentWindow.document.close();
                        frame.contentWindow.focus();
                        frame.contentWindow.document.execCommand('SaveAs', true, filename);

                        document.body.removeChild(frame);

                        setTimeout(function () {
                            URL.revokeObjectURL(downloadUrl);
                        }, 100); // cleanup
                    } else{
                        // error
                    }
                }
            }
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send(dataString);
        }
blub
  • 359
  • 6
  • 23

1 Answers1

0

You can't, not with XMLHTTPRequest and IE9.
You can just use the form to submit the request and set its target to an iframe, the response content disposition header is set to attachment it will cause the save dialog to open.

<form name="myForm" target="somehiddeniframe>
  ..
</form>
Musa
  • 96,336
  • 17
  • 118
  • 137