移动Web应用程序:在iOS上下载附件

时间:2012-10-23 11:27:43

标签: ios mobile-safari mobile-website

在我的移动网络应用程序中,我在用户中有一个页面可以查看附件。 附件可以是任何类型的文件(jpg,png,txt,doc,zip等)。 视图附件操作采用<a>标记的形式,指向处理请求的aspx文件。
HTML:

<a class="attachBtn" href="_layouts/ViewFile.aspx?messageAttachmentInstanceId={some id}"></a>


ViewFile.aspx:

public partial class ViewFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.IO.BinaryWriter bw = null;
            System.IO.MemoryStream ms = null;
            System.IO.StreamReader sr = null;

            try
            {

                string contentType = string.Empty;
                byte[] content = null;
                string fileName = string.Empty;

                if (!string.IsNullOrEmpty(Request.QueryString["messageAttachmentInstanceId"]) &&
                   !string.IsNullOrEmpty(Request.QueryString["messageInstanceId"]))
                {

                    int messageInstanceId = Int32.Parse(Request.QueryString["messageInstanceId"]);
                    Guid attachmentInstanceId;
                    GuidUtil.TryParse(Request.QueryString["messageAttachmentInstanceId"], out attachmentInstanceId);

                    MessageInstance messageInstance = WorkflowEngineHttpModule.Engine.GetService<IMessagingService>()
                        .GetMessageInstance(messageInstanceId);

                    if (messageInstance != null)
                    {
                        MessageAttachmentInstance attachmentInstnace = messageInstance.Attachments[attachmentInstanceId];
                        contentType = attachmentInstnace.ContentType;
                        fileName = attachmentInstnace.FileName;
                        content = attachmentInstnace.Content;
                    }
                }
                this.Response.ContentType = contentType;
                string headerValue = string.Format("attachment;filename={0}",
                    this.Server.UrlPathEncode(fileName));
                Response.AddHeader("content-disposition", headerValue);
                bw = new System.IO.BinaryWriter(this.Response.OutputStream);
                bw.Write(content);
            }

            catch (Exception ex)
            {
              LogError("ViewFile.aspx, "
                + ex.InnerException, ex);

            }
            finally
            {
                if (sr != null)
                    sr.Close();

                if (ms != null)
                    ms.Close();

                if (bw != null)
                    bw.Close();
            }
        }
    }


问题:
在Android设备中,当用户点击附件时,文件会自动下载,这是理想的行为,因为用户可以使用他想要的任何工具稍后打开文件,即使文件类型不受支持,用户也可以在以后下载可以打开的工具它。

但在iOS设备中,文件未下载,而是重定向到ViewFile.aspx并尝试在浏览器中打开文件,如果不支持文件类型,则显示警告:“safari无法下载此文件”。 即使支持文件类型,我也希望它能够下载而不是默认打开。

我怎样才能实现这种行为?

1 个答案:

答案 0 :(得分:1)

AFAIK,您无法在iOS上下载文件。

Safari(或已注册文件类型的任何应用程序,例如ZIP)支持的已知文件将打开或显示一个对话框,让用户选择如何打开文件。

您无法控制网络应用/网站的行为。