本文介绍了如何从一个模态对话框下载一个文件,该文件回发到自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net Web表单页面被包裹在一个巨大的形式,制约了页面上的其他形式,因为嵌套的形式不工作。这是故意的,因为在一个web表单项目中,只能有一个每页的形式,并且所有的服务器/客户端的交互需要它。为了建立一个自定义窗体,我放在一起这方式。为了让来自服务器的正确的错误讯息,我的模态对话框有回发到自身。为了做到这一点,我也跟着这一招使用表单对象为窗口的对话框中的名称。

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.

推荐答案

相当长的一段时间研究和建立一个小的测试案例后,我们发现有几个有趣的因素在这里。

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


  1. Response.WriteFile()工作在web表单罚款,甚至当窗体设置与当前窗口的目标。

  2. 模式对话框可以下载的文件完全没有问题,除非表单设置为当前窗口的目标。

经过广泛的测试,这似乎是一个不起眼的错误,或者在安全性甚至是一种奇怪的尝试。这也似乎是专门针对IE 8,虽然我没有测试IE的其他版本。

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

这修复了IE浏览器的bug,并出现在所有其他浏览器的工作(虽然我不能让歌剧显示弹出可言,甚至与允许所有弹出窗口)。

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.

这篇关于如何从一个模态对话框下载一个文件,该文件回发到自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:13