【问题标题】:download XSLX or DOCX file using XMLHTTPRequest response in IE 9 [duplicate]在 IE 9 中使用 XMLHTTPRequest 响应下载 XSLX 或 DOCX 文件 [重复]
【发布时间】:2016-11-01 21:41:15
【问题描述】:

如何在 IE 9 中使用 XMLHTTPRequest 响应下载 XSLX 或 DOCX 文件?我只需要 IE 9 解决方案。

此代码适用于纯文本和 csv 文件,但当我尝试下载 XSLX 或 DOCX 文件时没有任何反应。

响应来自一个 java servlet,其 response-content-type 设置为

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

我下载文件所需的javascript函数。

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

【问题讨论】:

    标签: javascript java internet-explorer servlets xmlhttprequest


    【解决方案1】:

    你不能,不是用 XMLHTTPRequest 和 IE9。
    您可以只使用表单提交请求并将其目标设置为 iframe,响应内容处置标头设置为附件将导致保存对话框打开。

    <form name="myForm" target="somehiddeniframe>
      ..
    </form>
    

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 1970-01-01
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2014-07-02
      • 2012-03-23
      相关资源
      最近更新 更多