How to download a file from a modal Dialog that posts back to itself?

流过昼夜 提交于 2020-01-07 07:41:13

问题


ASP.Net Webforms pages are wrapped in a massive form, which restricts other forms on the page because nested forms don't work. This is intentional because in a webforms project, there can only be one form per page, and all of the server/client interaction require it. In order to build a custom form, I put together a modal dialog in this way. In order to allow for proper error messages from the server, my modal dialog has to post back to itself. In order to do this, I followed this trick to use the form target as the name of the window the dialog is in.

The problem comes when I try to add a button to download a file. When I try to download a file from this dialog now, nothing happens. I can redirect my response to the file in question, but I can't write the file to the response because the dialog box just sits there when I click the button and nothing happens.


回答1:


After quite some time researching and building a small test case, I found out that there are several interesting factors here.

  1. Response.WriteFile() works fine in webforms, even when the form is set with a target of the current window.
  2. Modal dialogs can download files perfectly fine, unless the form is set to the target of the current window.

Through extensive testing, this appears to be an obscure bug, or perhaps even a strange attempt at security. This also appears to be specific to IE 8, though I didn't get to test other versions of IE.

The trick to allowing the download inside the modal dialog is to change the form's target.

function downloader() {
    document.getElementById("form1").target = "_blank";
}

This fixes the IE bug and appears to work in all other browsers (though I couldn't get opera to show the pop up at all, even with "allow all popups").

Thought my day of toiling shouldn't go to waste and maybe this will help someone else in the future.



来源:https://stackoverflow.com/questions/12858574/how-to-download-a-file-from-a-modal-dialog-that-posts-back-to-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!