title: Files stored in DB are "corrupt" when retrieved
body: <p>(ASP.NET 3.5 Web Forms app, using WCF services (Fluent NHibernate) for data access)</p>
  
  <p>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.</p>
  
  <p><strong>Upload Code:</strong></p>
  
  <pre><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();
  }
  </code></pre>
  
  <p><strong>Download Code:</strong></p>
  
  <pre><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();
  }
  </code></pre>
  
  <p><strong>Table:</strong></p>
  
  <pre><code>Id          int (PK)
  FooId       int (FK)
  FileName    varchar(100)
  FileBytes   varbinary(MAX)
  ContentType varchar(100)
  FileSize    bigint
  IsDeleted   bit
  </code></pre>
  
  <p>---------------------Update 2013.05.07---------------------</p>
  
  <p>After re-reading my post, I realize it may not be clear...I'm storing these files in the database.  </p>
  
  <p>When I retrieve the bytes from the database the byte[] always has a length of 8000, that doesn't seem right.</p>
  
  <p><strong>NHibernate Mapping:</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  -&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; 
      -&lt;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"&gt; 
          -&lt;id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="Id"/&gt; &lt;generator class="identity"/&gt; &lt;/id&gt; 
          -&lt;property name="ContentType" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="ContentType"/&gt; &lt;/property&gt; 
          -&lt;property name="CreatedBy" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="CreatedBy"/&gt; &lt;/property&gt; 
          -&lt;property name="DateCreated" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="DateCreated"/&gt; &lt;/property&gt; 
          -&lt;property name="FileBytes" type="System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="FileBytes"/&gt; &lt;/property&gt; 
          -&lt;property name="FileName" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="FileName"/&gt; &lt;/property&gt; 
          -&lt;property name="FileSize" type="System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="FileSize"/&gt; &lt;/property&gt; 
          -&lt;property name="IsDeleted" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="IsDeleted"/&gt; &lt;/property&gt; 
          -&lt;property name="LastModified" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="LastModified"/&gt; &lt;/property&gt; 
          -&lt;property name="LastModifiedBy" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"&gt; &lt;column name="LastModifiedBy"/&gt; &lt;/property&gt; 
          -&lt;many-to-one name="Foo" class="MyWebApp.Domain.Model.Foo, MyWebApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null"&gt; &lt;column name="FooId"/&gt; &lt;/many-to-one&gt; 
      &lt;/class&gt; 
  &lt;/hibernate-mapping&gt;
  </code></pre>