title: HttpPostedFileBase locking file
body: <p>I am uploading a file using <code>HttpPostedFileBase</code>.
  The file uploads without an issue, but when I try and read from that file (excel file), I get an error saying that I can not read from the file as it is locked. How do I remove the lock from the file / make it not lock the file in the first place?</p>
  
  <pre><code>[HttpPost]
          public ActionResult Index(HttpPostedFileBase file)
          {
              string path = string.Empty;
              if (file.ContentLength &gt; 0)
              {
                  var fileName = Path.GetFileName(file.FileName);
                  path = Path.Combine(Server.MapPath("~/App_Data/"), Guid.NewGuid() + fileName);
                  file.SaveAs(path);
                  file.InputStream.Flush(); 
  
                  file.InputStream.Close();
                  file.InputStream.Dispose();
  
                  // Import the file
                  ViewData["FileName"] = fileName;
                  //Added to ensure that the file had finished being written by the time      I try and read it
                  System.Threading.Thread.Sleep(2000);
              }
  
              return RedirectToAction("Index", new{fileName = path});
          }
  </code></pre>
  
  <p>The above code reads the file and saves it without issue, but the below fails to then load that file:</p>
  
  <blockquote>
    <p>var workbook = excelApp.Workbooks.Open(ExcelDocumentLocation,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value);</p>
  </blockquote>
  
  <p>I have confirmed that it is the fact that the file is locked as the issue by opening the file in excel and seeing the "File is locked by another user" message.</p>