(ASP.NET 3.5 Web Forms app, using WCF services (Fluent NHibernate) for data access)
I have an entity, let's call it Foo. Users can upload files as attachments to Foo. My upload and download functionality is "working" just fine...with one problem...when I open the files after downloading them I receive error messages informing me that the file is corrupt. The .txt, .docx and .xlsx files open anyway, after I click 'Ok' on the error msg, but all other file types fail to open. Google hasn't been much help, I'm hoping someone here might be able to point me in the right direction.
Upload Code:
// 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();
}
Download Code:
...
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();
}
Table:
Id int (PK)
FooId int (FK)
FileName varchar(100)
FileBytes varbinary(MAX)
ContentType varchar(100)
FileSize bigint
IsDeleted bit
---------------------Update 2013.05.07---------------------
After re-reading my post, I realize it may not be clear...I'm storing these files in the database.
When I retrieve the bytes from the database the byte[] always has a length of 8000, that doesn't seem right.
NHibernate Mapping:
<?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>