Android 忽略内容处置 : Attachment Inside Iframe

标签 android html http http-headers

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

在您告诉我停止使用 iframe 之前,我恐怕目前无法选择。

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

  1. Default.aspx:包含指向 DownloadPage.aspx 的 iframe
  2. DownloadPage.aspx:包含指向 Download.ashx 的 super 链接
  3. Download.ashx:响应带有内容配置的文本/纯内容类型:附件

默认.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 忽略内容处置 : Attachment Inside Iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12352424/

相关文章:

java - 不关闭 URLConnection

Java:如何监听php发送的http POST请求?

android - genymotion 在没有 opengl 的虚拟系统中,这可能吗?

java - Android系统应用

html - 字体在 IE7 和 8 中看起来更大 - 但只有一个网站的一页

javascript - GetElementsByTagName ▪ item() { [本地代码] } - 这是什么?

javascript - 在三个 CSS3 列中延迟加载图像

Python 'requests' 库 - 定义特定的 DNS?

java - OnTouchListener 坐标不正确

java - 在 Android Studio 中执行扩展 AndroidTestCase 的测试类