2012-09-10 63 views
3

我有一個鏈接到iframe中的下載處理程序。該下載處理程序在頭中具有「Content-Disposition:Attachment」以強制用戶下載文件。這適用於所有瀏覽器,Android設備上的除外。 Android忽略文件沒有任何錯誤或消息。使用Fiddler,我可以確認處理程序獲得成功請求,&下載似乎成功完成,但瀏覽器不讓我保存或打開文件。Android忽略內容處置:Iframe內的附件

在您告訴我停止使用iframe之前,恐怕目前這不是我的選擇。

下面是一些重現此問題的代碼。三個文件:

  1. 的Default.aspx:包含一個iframe指向DownloadPage.aspx
  2. DownloadPage.aspx:包含hyperpink到Download.ashx
  3. Download.ashx:用純文本/內容 - 迴應型類型與內容處置:附件

Default.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> 

DownloadPage.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> 

Download.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; 
      } 
     } 
    } 
} 

感謝您的時間。

回答

0

大多數瀏覽器都將這樣做作爲安全的一點以避免惡意嵌入式自動下載。您可能希望向用戶提供鏈接或按鈕以完成下載,以便瀏覽器識別交互並允許下載完成。