ashx文件下载,导致firefox出现问题

时间:2011-06-24 10:06:03

标签: .net asp.net ashx

创建了一个ashx页面,称为

<a href="AttachmentHandler.ashx?id=2">download</a>

在IE上它工作正常,但在Firefox上某些类型的文件(doc / xslx等)它会破坏文件,比如改变大小。我试图使用编码/字符集选项,但它无法正常工作

<%@ 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.Response.ContentType = "text/plain";
            context.Response.Write(Resources.GlobalResource.ErrorMessageUnableAttachements);
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

4 个答案:

答案 0 :(得分:2)

添加

context.Response.ContentType

在您的此代码中,您的错误将被解决...

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();
            }

答案 1 :(得分:0)

我认为你不应该这样称呼:context.Response.Close();

答案 2 :(得分:0)

上次我必须编写类似的代码:

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();

它适用于FF ... 也许.Flush()稍微关闭了流......

我注意到的另一件事:我使用一段代码来说明ContentType是什么..对于Msword,我得到application/unknown 但无论如何似乎工作正常..

答案 3 :(得分:0)

.docx不是application/msword。它是application/vnd.openxmlformats-officedocument.wordprocessingml.document

检查您是否使用了.docX和.xslX文件的正确MIME类型。