title: ASP.NET FileUpload Cannot access a closed file
body: <p>I'm aware there are several other questions like this on SO, however every answer I've tried so far from a range of websites, SO, MSDN etc etc, have all proven to have no impact on the error.</p>
  
  <p>I've got an <code>asp:FileUpload</code> which, on change, I'm storing in a session variable (I have other fields which cause post back's so need to ensure that I've got the file the user's uploaded somewhere).</p>
  
  <p>The error I am getting is </p>
  
  <pre><code>{"Cannot access a closed file."}
  Data: {System.Collections.ListDictionaryInternal}
  HResult: -2146232798
  HelpLink: null
  InnerException: null
  Message: "Cannot access a closed file."
  ObjectName: ""
  Source: "mscorlib"
  StackTrace: "   at System.IO.__Error.FileNotOpen()\r\n   at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)\r\n   at System.Web.HttpRawUploadedContent.TempFile.GetBytes(Int32 offset, Int32 length, Byte[] buffer, Int32 bufferOffset)\r\n   at System.Web.HttpRawUploadedContent.CopyBytes(Int32 offset, Byte[] buffer, Int32 bufferOffset, Int32 length)\r\n   at System.Web.HttpInputStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n   at GCSearchPortal.GlobalFunctions.UploadFile[T](HttpServerUtility server, FileType type, T file, Int32 id) in C:\\Users\\zach.ross-clyne\\Source\\Workspaces\\GC Search Portal\\GCSearchPortal\\GCSearchPortal\\GlobalFunctions.cs:line 241"
  TargetSite: {Void FileNotOpen()}
  </code></pre>
  
  <p>This is what's in my web.config in the <code>system.web</code> section</p>
  
  <pre><code>&lt;httpRuntime useFullyQualifiedRedirectUrl="true" maxRequestLength="1048576" requestLengthDiskThreshold="1073741824"/&gt;
  </code></pre>
  
  <p>The code that is causing the issue is this:</p>
  
  <pre><code>internal static int UploadFile&lt;T&gt;(HttpServerUtility server, FileType type, T file, int id)
  {
      if (typeof(T) == typeof(HtmlInputFile) || typeof(T) == typeof(FileUpload))
      {
          string fileName = null;
          string location = null;
  
          dynamic fileUpload = file;
  
          if (fileUpload.PostedFile != null &amp;&amp; fileUpload.PostedFile.ContentLength &gt; 0)
          {
              try
              {
                  /* Get a reference to PostedFile object */
                  HttpPostedFile attFile = fileUpload.PostedFile;
  
                  /* Get size of the file */
                  int attachFileLength = attFile.ContentLength;
                  /* Make sure the size of the file is &gt; 0  */
                  if (attachFileLength &gt; 0)
                  {
                      if (attachFileLength &lt; 52428800)
                      {
                          /* Get extension for file */
                          string ext = Path.GetExtension(attFile.FileName);
  
                          switch (type)
                          {
                              case FileType.TermsOfBusiness:
                                  location = "~/Clients/" + id + "/";
                                  fileName = "TOB";
                                  break;
                              case FileType.TermsOfRole:
                                  location = "~/Jobs/" + id + "/";
                                  fileName = "TOR";
                                  break;
                              case FileType.CandidateCV:
                                  location = "~/Candidates/" + id + "/";
                                  fileName = "CV";
                                  break;
                              case FileType.NewCV:
                                  location = "~/CVs/";
                                  fileName = id.ToString();
                                  break;
                          }
  
                          fileName += ext;
  
                          /* Save the file on the server */
                          System.IO.Directory.CreateDirectory(server.MapPath(location));
  
                          string filePath = attFile.FileName;
                          string fileName2 = fileUpload.FileName;
                          Stream fStream = fileUpload.FileContent;
                          byte[] contents = new byte[fStream.Length];
  
                          fStream.Read(contents, 0, (int)fStream.Length); // &lt;&lt;&lt;&lt;&lt; This line causes the crash
  
                          File.WriteAllBytes(server.MapPath(location + fileName), contents);
  
                          // This is what the code was at the very beginning before all my changes
                          //attFile.SaveAs(server.MapPath(location + fileName));
  
                          return 2;
                      }
                      else
                      {
                          return 3;
                      }
                  }
                  else
                  {
                      return 1;
                  }
              }
              catch (Exception ex)
              {
                  return 1; // &lt;&lt;&lt;&lt;&lt; This is where it's crashing and giving the error above
              }
          }
          else
          {
              return 0;
          }
      }
      else
      {
          throw new Exception("Passed type is not supported");
      }
  }
  </code></pre>
  
  <p>Thanks for any help in advance</p>