【问题标题】:Android Ignores Content-Disposition: Attachment Inside IframeAndroid 忽略 Content-Disposition:Iframe 内的附件
【发布时间】:2012-09-03 08:42:50
【问题描述】:

我有一个 iframe 内下载处理程序的链接。此下载处理程序在标头中有“Content-Disposition: Attachment”以强制用户下载文件。这适用于 Android 设备上的所有浏览器除了。 Android 会忽略该文件,而不会出现任何错误或消息。使用 Fiddler,我可以确认处理程序已成功请求,并且下载似乎成功完成,但浏览器不允许我保存或打开文件。

在你告诉我停止使用 iframe 之前,我恐怕目前不适合我。

以下是重现此问题的一些代码。三个文件:

  1. Default.aspx:包含一个指向 DownloadPage.aspx 的 iframe
  2. DownloadPage.aspx:包含指向 Download.ashx 的超粉红色
  3. Download.ashx:以 text/plain 内容类型类型响应,内容处置:附件

默认.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AndroidTest.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <iframe src="DownloadPage.aspx" width="320" height="1000"></iframe>
    </div>
    </form>
</body>
</html>

下载页面.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DownloadPage.aspx.cs" Inherits="AndroidTest.DownloadPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink NavigateUrl="~/Download.ashx" runat="server">Download</asp:HyperLink>
    </div>
    </form>
</body>
</html>

下载.ashx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AndroidTest {
    /// <summary>
    /// Summary description for Download
    /// </summary>
    public class Download : IHttpHandler {

        public void ProcessRequest(HttpContext context) {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("Content-Disposition", "attachment;filename=\"test.txt\"");
            context.Response.Write("Hello World");
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }
}

感谢您的宝贵时间。

【问题讨论】:

    标签: android html http http-headers


    【解决方案1】:

    大多数浏览器都会这样做作为安全点,以避免恶意嵌入的自动下载。您可能希望向用户显示链接或按钮以完成下载,以便浏览器识别交互并允许下载完成。

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-29
      • 2011-03-16
      • 2016-12-18
      • 1970-01-01
      相关资源
      最近更新 更多