【发布时间】:2012-09-03 08:42:50
【问题描述】:
我有一个 iframe 内下载处理程序的链接。此下载处理程序在标头中有“Content-Disposition: Attachment”以强制用户下载文件。这适用于 Android 设备上的所有浏览器除了。 Android 会忽略该文件,而不会出现任何错误或消息。使用 Fiddler,我可以确认处理程序已成功请求,并且下载似乎成功完成,但浏览器不允许我保存或打开文件。
在你告诉我停止使用 iframe 之前,我恐怕目前不适合我。
以下是重现此问题的一些代码。三个文件:
- Default.aspx:包含一个指向 DownloadPage.aspx 的 iframe
- DownloadPage.aspx:包含指向 Download.ashx 的超粉红色
- 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