简体   繁体   中英

Mobile web application: download attachments on iOS

In my mobile web application i have one page within user can view attachments. The attachment can be any type of file (jpg,png,txt,doc,zip, etc). The view attachment action is in the form of <a> tag that points to an aspx file that process the request.
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();
            }
        }
    }


The Problem:
in Android devices when user click on attachment the file is downloaded automatically which is the desirable behavior because the user can open the file later with any tool he wants and even if the file type is not supported user can later on download a tool which can open it.

but in iOS devices the file is not downloaded but instead redirects to ViewFile.aspx and tries to open the file within the browser and if the file type is not supported it shows alert: "safari cannot download this file". even if the file type is supported i want it to be downloaded and not open by default.

How can i achieve this behavior?

AFAIK, you cannot download files on iOS.

Known files that Safari (or any app that has registered a file type, eg ZIP) supports will open or show a dialog letting the user choose how to open the file.

You can't control the behavior from your web app/site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM