IE 7 的错误? - 下载文件时提示保存/打开 - c# asp.net 3.5

发布于 2024-08-01 13:31:11 字数 1006 浏览 12 评论 0原文

我有一个带有链接按钮的 aspx 页面,可以触发 javascript 弹出打开一个新的 aspx 页面,将文件流式传输到浏览器供用户下载。

在XP SP3、IE 7和FireFox 3.5上进行开发和单元测试时,使用以下代码(关键是Content-Disposition标签中的“附件”部分),都会提示一个对话框,询问我是否要保存或打开文档,这正是我想要发生的事情:

        private void WriteFileToBrowser(Byte[] requestFile, string filename, String m_mimeType, String m_format)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + "." + m_format);
        Response.ContentType = m_mimeType;
        Response.BinaryWrite(requestFile);
        Response.Flush();

    }

当我将其部署到 Windows 2003 服务器并导航到相同的 aspx 页面时,FireFox 3.5 正确地要求按预期提供“保存/打开”选项,因为这是 FF 中的默认操作。

然而,当我在 IE 7 中导航并单击下载时,我会看到一个弹出窗口,该窗口在 1/8 秒内可见......然后消失。 没有提示保存/打开。

如果我进入 IE 7 -> 工具-> 互联网选项 -> 安全-> 自定义级别 -> 下载

文件下载的自动提示已禁用。 当我检查它以启用时,我会收到“保存/打开”提示以正常工作。

所以我的问题是……有人解决这个问题了吗? 我已经尝试了很多人们声称可以使用不同的标头标签(例如缓存、pragma 等)的东西……这些都没有回避 IE 默认禁用下载属性的事实。

I have an aspx page with linkbuttons that fire javascript to pop open a new aspx page to stream files to the browser for downloading by users.

When developing and unit testing on XP SP3, IE 7 and FireFox 3.5, using the following code (key being the "attachment" part in the Content-Disposition tag), both prompt a dialog box asking if I want to save or open the document, which is exactly what I want to happen:

        private void WriteFileToBrowser(Byte[] requestFile, string filename, String m_mimeType, String m_format)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + "." + m_format);
        Response.ContentType = m_mimeType;
        Response.BinaryWrite(requestFile);
        Response.Flush();

    }

When I deploy this to a Windows 2003 server and navigate to the same aspx page, FireFox 3.5 correctly asks for a Save/Open option as expected since that is the default operation in FF.

When I navigate in IE 7 however and click to download, i get a pop up window that is visible for 1/8th second tops...and disappears. No prompt to Save/Open.

If i go into IE 7 -> Tools -> Internet Options -> Security -> Custom Level -> Downloads

Automatic prompting for file downloads is disabled. When i check it to enable i then get the Save/Open prompt to work correctly.

So my question is.....has anyone gotten a work around to this? I have tried a bunch of things people claim work with different Header tags such as cache, pragma, etc etc...none of that gets around the fact that IE has the download property disabled by default.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风无影 2024-08-08 13:31:11

与此问题无关,但您需要在输出中引用文件名。 下载文件时,文件名中的空格会导致文件名混乱。

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "." + m_format +"\"");

Unrelated to this problem but you need to quote the file name in your output. Spaces in the file name will screw up the file name when downloading the file.

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "." + m_format +"\"");
吝吻 2024-08-08 13:31:11

如果不需要使用 javascript 打开新页面,则可以对 aspx 页面使用 Response.Redirect()。 这应该打开保存/打开对话框。

If it's not a necessity that a new page is opened using javascript, you can use a Response.Redirect() to the aspx page. This should open the save/open dialog.

情归归情 2024-08-08 13:31:11

试试这个

protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "应用程序/pdf";
Response.Clear();
Response.TransmitFile("test.pdf");
响应.End();
}

Try This

protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.Clear();
Response.TransmitFile("test.pdf");
Response.End();
}

灼痛 2024-08-08 13:31:11

如果您将页面添加到受信任的站点,您将能够下载该文件。
在开发您正在运行的所有网站时,您都在这个区域中。

强制 Internet Explorer

HttpContext.Current.ApplicationInstance.CompleteRequest();

您可以尝试通过添加到请求末尾来

。 否则,您可以尝试将链接发布到文档,以便浏览器将请求视为用户交互的响应。

If you would add your page to the trusted sites you would be able to download the file.
While developing all your sites you're running are in this zone.

You can try to force the internet explorer by adding

HttpContext.Current.ApplicationInstance.CompleteRequest();

to the end of your request.

Else you could try to post the link to the document, so the browser sees the request as a response of user interaction.

兲鉂ぱ嘚淚 2024-08-08 13:31:11

最近,我们在自定义 Web 框架中实现下载生成的报告时遇到了同样的问题。 研究使我们尝试了您提到的相同方法(设置内容处置)。

该问题与 IE7 和安全区域有关。 默认情况下,某些操作必须由用户显式启动。 您可以首先查看 了解并在保护模式 Internet 中工作资源管理器关于窗口限制

We ran into an identical problem recently with our implementation for downloading generated reports in our custom web framework. Research led us to trying the same approach you mentioned (setting Content-Disposition).

The problem is related to IE7 and security zones. By default, certain actions MUST be explicitly initiated by the user. You could start by looking at Understanding and Working in Protected Mode Internet Explorer and About Window Restrictions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文