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