0

基本上我试图触发动态构建的文件的下载。为了构造文件,我需要将大量信息传递给 servlet。

这意味着我不能使用 GET,而应该使用 POST。

但是,为了使下载不会弄乱应用程序,我必须打开一个新窗口。问题是我不能这样做: window.open( http://very.long.url.com/.../ , "_blank");

因为这会导致超出 URL 限制并使用 GET 而不是 POST。

所有需要发送到 servlet 的信息都在一个名为“config.xml”的对象中。

我的解决方案是使用 POST 方法在新窗口中编写一个表单,然后向其中添加隐藏的输入,然后触发提交,如下所示:

var win = window.open("about:blank", "_blank");
win.document.write("<form method='post' id=donwload action='serveletURL'></form>");

$.each(config, function (name, value) {
    var fragment = document.createDocumentFragment(),
    temp = document.createElement('div');
    temp.innerHTML = '<input type=\'hidden\' name=\'' + name + '\' value=\'' + value + '\'></input>';
    while (temp.firstChild) {
        fragment.appendChild(temp.firstChild);
    }

    win.document.getElementById("download").appendChild(fragment);
});

win.document.getElementById('download').submit();

它适用于 Google Chrome 和 Firefox,但不适用于 IE。在 IE(甚至 IE 10)上,会打开一个带有“serveletURL”文件名的下载对话框,当我要求保存或打开文件时,不会下载任何内容。

我确定我错过了一些东西......我该如何做到这一点?

谢谢!爱德华多

4

0 回答 0