(ASP.NET 3.5 Web Forms应用程序,使用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 --------------------- < / p>
重新阅读我的帖子后,我意识到它可能不太清楚......我将这些文件存储在数据库中。
当我从数据库中检索字节时,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>
答案 0 :(得分: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在提出问题之前没有出现在我的搜索结果中。我为复制道歉。