移动web应用程序:在iOS上下载附件
在我的移动web应用程序中,我有一个用户可以查看附件的页面。 附件可以是任何类型的文件(jpg、png、txt、doc、zip等)。 查看附件操作的形式为移动web应用程序:在iOS上下载附件,ios,mobile-safari,mobile-website,Ios,Mobile Safari,Mobile Website,在我的移动web应用程序中,我有一个用户可以查看附件的页面。 附件可以是任何类型的文件(jpg、png、txt、doc、zip等)。 查看附件操作的形式为 ViewFile.aspx: public部分类视图文件:System.Web.UI.Page { 受保护的无效页面加载(对象发送方、事件参数e) { System.IO.BinaryWriter bw=null; System.IO.MemoryStream ms=null; System.IO.StreamReader sr=nul
ViewFile.aspx:
public部分类视图文件:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
System.IO.BinaryWriter bw=null;
System.IO.MemoryStream ms=null;
System.IO.StreamReader sr=null;
尝试
{
string contentType=string.Empty;
字节[]内容=空;
字符串文件名=string.Empty;
如果(!string.IsNullOrEmpty(Request.QueryString[“messageAttachmentInstanceId”])&&
!string.IsNullOrEmpty(Request.QueryString[“messageInstanceId”]))
{
int messageInstanceId=Int32.Parse(Request.QueryString[“messageInstanceId”]);
Guid attachmentInstanceId;
TryParse(Request.QueryString[“messageAttachmentInstanceId”],out attachmentInstanceId);
MessageInstance MessageInstance=WorkflowEngineHttpModule.Engine.GetService()
.GetMessageInstance(messageInstanceId);
if(messageInstance!=null)
{
MessageAttachmentInstance AttachmentInstance=messageInstance.Attachments[attachmentInstanceId];
contentType=AttachmentInstance.contentType;
fileName=attachmentinstance.fileName;
content=附件instance.content;
}
}
this.Response.ContentType=ContentType;
string headerValue=string.Format(“附件;文件名={0}”),
this.Server.urlpath编码(文件名));
AddHeader(“内容处置”,headerValue);
bw=new System.IO.BinaryWriter(this.Response.OutputStream);
写(内容);
}
捕获(例外情况除外)
{
日志错误(“ViewFile.aspx,”
+例(例);
}
最后
{
如果(sr!=null)
高级关闭();
如果(毫秒!=null)
Close女士();
如果(bw!=null)
bw.Close();
}
}
}
问题:
在Android设备中,当用户点击附件时,文件会自动下载,这是一种理想的行为,因为用户可以稍后使用任何他想要的工具打开文件,即使文件类型不受支持,用户也可以稍后下载一个可以打开文件的工具。
但在iOS设备中,该文件不会下载,而是重定向到ViewFile.aspx并尝试在浏览器中打开该文件,如果不支持该文件类型,则会显示警报:“safari无法下载此文件”。 即使支持该文件类型,我也希望在默认情况下将其下载而不是打开。
如何实现此行为?好的,您不能在iOS上下载文件 Safari(或任何已注册文件类型的应用程序,如ZIP)支持的已知文件将打开或显示一个对话框,让用户选择如何打开该文件
您无法从web应用程序/网站控制行为。我想我可以使用对话框,让用户选择如何打开文件。但是没有对话框…不,只有在支持该文件时才会显示对话框。看看这个:
<a class="attachBtn" href="_layouts/ViewFile.aspx?messageAttachmentInstanceId={some id}"></a>
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();
}
}
}