2

(ASP.NET 3.5 Web 窗体应用程序,使用 WCF 服务 (Fluent NHibernate) 进行数据访问)

我有一个实体,我们称之为 Foo。用户可以将文件作为附件上传到 Foo。我的上传和下载功能“工作”得很好......有一个问题......当我下载文件后打开文件时,我收到错误消息,通知我文件已损坏。.txt、.docx 和 .xlsx 文件在我单击错误消息上的“确定”后仍然打开,但所有其他文件类型都无法打开。谷歌并没有太大的帮助,我希望这里的人能指出我正确的方向。

上传代码:

// Attachment is a FileUpload control
if (Attachment.HasFile)
{
    HttpPostedFile file = Attachment.PostedFile;
    FooContract foo = FooService.LookupFoo(FooId.Value, CurrentUser.Id);
    int contentLength = file.ContentLength;

    // Allocate a buffer for reading the file
    byte[] fileData = new byte[contentLength];
    // Read uploaded file from the Stream
    file.InputStream.Read(fileData, 0, contentLength);

    FooAttachmentContract attachment = new FooAttachmentContract();
    attachment.FileSize = contentLength.ToString();
    attachment.FileName = Path.GetFileName(Attachment.FileName);
    attachment.ContentType = file.ContentType;
    attachment.FooId = foo.Id;
    _attachments.Add(FooService.AddFooAttachment(attachment, fileData, CurrentUser.Id));
    // Clear cache
    Session["Attachments"] = null;
    // Rebind grid
    BindAttachmentGrid();
}

下载代码:

...
if([user has permission])
{
    DownloadFileResponse response = FooService.RetrieveAttachment(attachmentId, CurrentUser.Id);
    DownloadAttachment(response.Attachment.ContentType, response.Attachment.FileName, response.FileBytes);
}
else
    AccessDenied();
...


protected void DownloadAttachment(string mimeType, string fileName, byte[] file)
{
    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition", string.Format("attachment;filename=\"{0}\"", fileName));
    Response.BinaryWrite(file);
    Response.Flush();
    Response.End();
}

桌子:

Id          int (PK)
FooId       int (FK)
FileName    varchar(100)
FileBytes   varbinary(MAX)
ContentType varchar(100)
FileSize    bigint
IsDeleted   bit

---------------------更新 2013.05.07---------

重新阅读我的帖子后,我意识到可能不清楚......我将这些文件存储在数据库中。

当我从数据库中检索字节时,byte[] 的长度总是 8000,这似乎不对。

NHibernate 映射:

<?xml version="1.0"?>
-<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"> 
    -<class xmlns="urn:nhibernate-mapping-2.2" table="`FooAttachment`" name="MyWebApp.Domain.Model.FooAttachment, MyWebApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null"> 
        -<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="Id"/> <generator class="identity"/> </id> 
        -<property name="ContentType" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="ContentType"/> </property> 
        -<property name="CreatedBy" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="CreatedBy"/> </property> 
        -<property name="DateCreated" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="DateCreated"/> </property> 
        -<property name="FileBytes" type="System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="FileBytes"/> </property> 
        -<property name="FileName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="FileName"/> </property> 
        -<property name="FileSize" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="FileSize"/> </property> 
        -<property name="IsDeleted" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="IsDeleted"/> </property> 
        -<property name="LastModified" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="LastModified"/> </property> 
        -<property name="LastModifiedBy" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <column name="LastModifiedBy"/> </property> 
        -<many-to-one name="Foo" class="MyWebApp.Domain.Model.Foo, MyWebApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null"> <column name="FooId"/> </many-to-one> 
    </class> 
</hibernate-mapping>
4

1 回答 1

0

阅读stackoverflow.com/questions/4584170后,我更改了我的 FooAttachment 映射类。现在我可以毫无问题地下载附件。

这是我的新 FooAttachmentMap 类:

public class FooAttachmentMap : ClassMap<FooAttachment>
{
    public FooAttachmentMap()
    {
        Id(x => x.Id);
        Map(x => x.ContentType);
        Map(x => x.CreatedBy);
        Map(x => x.DateCreated);
        Map(x => x.FileBytes)
            .Length(int.MaxValue);
        Map(x => x.FileName);
        Map(x => x.FileSize);
        Map(x => x.IsDeleted);
        Map(x => x.LastModified);
        Map(x => x.LastModifiedBy);
        References(x => x.Foo)
            .Column("FooId");
    }
}

(在问我的问题之前,不知何故stackoverflow.com/questions/4584170没有出现在我的搜索结果中。我为重复道歉)。

于 2013-05-13T16:58:54.323 回答