ashx file download, causing problem in firefox
Created a ashx page, which is called like
<a href="AttachmentHandler.ashx?id=2">download</a>
On IE it is working fine, but on firefox for some type of file(doc/xslx etc) it currupt the file, like change in size. i tried to use encodeing/charset options but it is not working
<%@ WebHandler Language="C#" Class="AttachmentHandler" %>
using System;
using System.Web;
public class AttachmentHandler : IHttpHandler, System.Web.SessionState.IReadOnlySessionState{
public void ProcessRequest(HttpContext context)
{
try
{
string attachmentId = context.Request["Id"];
if (!string.IsNullOrEmpty(attachmentId))
{
Attachment attachmentEntity = AttachmentProvider.GetById(Convert.ToInt32(attachmentId));
if (attachmentEntity != null)
{
context.Response.Clear();
//context.Response.ClearHeaders();
//context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//context.Response.Charset = System.Text.Encoding.UTF8.WebName;
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Buffer = false;
context.Response.ContentType = attachmentEntity.AttachmentType;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + attachmentEntity.Name + "\"");
context.Response.BinaryWrite(attachmentEntity.AttachmentFile);
context.Response.Flush();
context.Response.Close();
}
}
else
{
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ContentType = "text/plain";
context.Response.Write(Resources.GlobalResource.ErrorMessageUnableAttachements);
}
}
catch (Exception ex)
{
IQity.Fusion.Utility.LoggingUtility.IQityErrorLogger.HandleException(ex);
context.Response.Clear();
context.Response.ClearHeaders();
context.Respo开发者_开发技巧nse.ContentType = "text/plain";
context.Response.Write(Resources.GlobalResource.ErrorMessageUnableAttachements);
}
}
public bool IsReusable {
get {
return false;
}
}
}
Add
context.Response.ContentType
in your this code and ur error will resolved...
if (attachmentEntity != null)
{
context.Response.Clear();
//context.Response.ClearHeaders();
//context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//context.Response.Charset = System.Text.Encoding.UTF8.WebName;
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Buffer = false;
context.Response.ContentType = attachmentEntity.AttachmentType;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + attachmentEntity.Name + "\"");
context.Response.BinaryWrite(attachmentEntity.AttachmentFile);
context.Response.Flush();
context.Response.Close();
}
I don't think you are supposed to call this: context.Response.Close();
The last time I had to code something similar :
context.Response.Clear();
context.Response.ContentType = attachmentEntity.AttachmentType;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + attachmentEntity.Name + "\"");
context.Response.BinaryWrite(attachmentEntity.AttachmentFile);
context.Response.End();
and it works with FF...
maybe the .Flush()
is closing the stream a little too earlier...
One other thing I noticed : I use a piece of code to tell what is the ContentType.. and for Msword, I get application/unknown
but seems to work fine anyway..
.docx
is not application/msword
. It's application/vnd.openxmlformats-officedocument.wordprocessingml.document
.
Check you're using the right MIME type for .docX and .xslX files.
精彩评论