【问题标题】:Mobile web application: download attachments on iOS移动 Web 应用程序:在 iOS 上下载附件
【发布时间】:2012-10-23 11:27:42
【问题描述】:

在我的移动网络应用程序中,我有一个用户可以查看附件的页面。 附件可以是任何类型的文件(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 无法下载此文件”。 即使支持文件类型,我也希望它被下载而不是默认打开。

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

【问题讨论】:

    标签: ios mobile-safari mobile-website


    【解决方案1】:

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

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多