title: MVC3 HttpPostedFileBase First Upload Gives No Data But Subsequent Do
body: <p>Background of the issue.</p>
  
  <p><strong>EDIT 3</strong>
  I can verify that this appears to be working again in Chrome 20. Thanks!</p>
  
  <p><strong>TLDR Update</strong></p>
  
  <p><strong>EDIT 2</strong>
  After even further mucking about this appears to be a bug in Chrome 19. Please see here:</p>
  
  <p><a href="http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/15bc4992bcd4be1d/b905e8de20ee58fa?lnk=raot" rel="nofollow noreferrer">http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/15bc4992bcd4be1d/b905e8de20ee58fa?lnk=raot</a></p>
  
  <p>Nothing like an up to the minute bug! :-)</p>
  
  <p>Firefox-latest and IE8/9 work as expected. The Fiddler logs show the same behavior with the major difference that the 2nd 401 authorization check does not send the form payload garbled and it works as expected.</p>
  
  <p>It sounds like a test case and a fix is in the works! So, hopefully for any of you out there that may have run into this, this helps!</p>
  
  <p><strong>END TLDR</strong></p>
  
  <p>I have a controller action that takes in solely an uploaded file which is a CSV file of "events". Behind the scenes, this CSV gets parsed out, turned into Event objects and the data is sync'd in the database. As a result output, the user is prompted to download an Excel spreadsheet that contains all the errors on each line that occurred.</p>
  
  <p>This all works fine locally on my development machine. Once deploying to the DEV environment however, I'm getting different results. The first attempt to upload a file results in the stream being, what's the word that I'm looking for here, unreadable? It correctly reports its length but attempts to pull the information out gets nothing. Subsequent reads do have the data and everything works as expected.</p>
  
  <p>I'll give to you the pertinent code pices. First, the gen'd HTML form simplified:</p>
  
  <pre><code>&lt;form action="/Events/ImportLocal" enctype="multipart/form-data" method="post"&gt;
  
  &lt;input id="uploadFile" name="uploadFile" type="file" /&gt;
  &lt;input type="submit" value="Upload Events" /&gt;    
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Pretty straight forward:</p>
  
  <p>Controller action (forgive the mess, I've been hacking at this for a couple hours now):</p>
  
  <pre><code>[HttpPost]
  public ActionResult ImportLocal(HttpPostedFileBase uploadFile)
  {
      if (uploadFile == null || uploadFile.ContentLength &lt;= 0)
      {
          BaseLogger.Info("uploadFile is null or content length is zero...");
          //Error content returned to user
      }
  
      BaseLogger.InfoFormat("Community Import File Information: Content Length: {0}, Content Type: {1}, File Name: {2}",
      uploadFile.ContentLength, uploadFile.ContentType, uploadFile.FileName);
  
  //This logger line is always reporting as correct. Even on the attempts where I can't pull out the stream data, I'm seeing valid values here.
  //EXAMPLE output on failed attempts:
  //Import File Information: Content Length: 315293, Content Type: application/vnd.ms-excel, File Name: Export_4-30-2012.csv
  
      BaseLogger.InfoFormat("Upload File Input Stream Length: {0}", uploadFile.InputStream.Length);           
  //This is reporting the correct length on failed attempts as well
  
  
  //I know the below is overly complicated and convoluted but I'm at a loss for why the inputstream in HttpPostedFileBase is not pulling out as expected
  //so I've been testing
          var target = new MemoryStream();
          uploadFile.InputStream.CopyTo(target);
          byte[] data = target.ToArray();
          BaseLogger.InfoFormat("File stream resulting length = {0}", data.Length);
  //This reports correctly. So far so good.
  
          StringReader stringOut;
          var stream = new MemoryStream(data) {Position = 0};
  
          using (var reader = new StreamReader(stream))
          {
              string output = reader.ReadToEnd();
              BaseLogger.InfoFormat("Byte[] converted to string = {0}", output);
  //No go...output is reported to be empty at this point so no data ever gets sent on to the service call below
              stringOut = new StringReader(output);
          }
  
  
          //Build up a collection of CommunityEvent objects from the CSV file
          ImportActionResult&lt;Event&gt; importActionResults = _eventImportServices.Import(stringOut);
  </code></pre>
  
  <p>The goal is to pass a textreader or a stringreader to the service method as behind the scenes this is using a CSV processing implementation that wants these types.</p>
  
  <p>Any ideas why the first request fails but subsequent work? I feel I need a fresh set of eyes as I'm running dry on ideas.</p>
  
  <p>Last point, IIS 7.5 is the target both locally via IIS Express and on the destination server.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong> for the bounty: </p>
  
  <p>I'm copying my bounty message here and the Fiddler output for each request</p>
  
  <p>I've added a few comments to this question. The issue appears related to NTLM. What I see in Fiddler is a 401 Unauthorized on request 1 with valid form data (point, it's my understanding that these first two 401 Unauthorized requests are "normal" under scenarios where only Windows Authentication is on; verify please?). Request 2 listed is again a 401 with the same form data except the uploaded file data is now just a bunch of boxes, the exact same size worth of data, just not the exact uploaded data. Request 3 is a 200 OK that contains the garbled data and this is what my controller action is getting. How do I get NTLM to play nice with file uploads?</p>
  
  <p>Here's the Fiddler output for each request:</p>
  
  <p>Request 1)
  <img src="https://i.stack.imgur.com/87ROx.png" alt="enter image description here"></p>
  
  <p>Request 2)
  <img src="https://i.stack.imgur.com/7y7gy.png" alt="enter image description here"></p>
  
  <p>And finally request 3 that is the processed HTTP 200 OK
  <img src="https://i.stack.imgur.com/aoYjn.png" alt="enter image description here"></p>
  
  <p><strong>EDIT 2</strong>
  After even further mucking about this appears to be a bug in Chrome 19. Please see here:</p>
  
  <p><a href="http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/15bc4992bcd4be1d/b905e8de20ee58fa?lnk=raot" rel="nofollow noreferrer">http://groups.google.com/a/chromium.org/group/chromium-bugs/browse_thread/thread/15bc4992bcd4be1d/b905e8de20ee58fa?lnk=raot</a></p>
  
  <p>Nothing like an up to the minute bug! :-)</p>
  
  <p>Firefox-latest and IE8/9 work as expected. The Fiddler logs show the same behavior with the major difference that the 2nd 401 authorization check does not send the form payload garbled and it works as expected.</p>
  
  <p>It sounds like a test case and a fix is in the works! So, hopefully for any of you out there that may have run into this, this helps!</p>
  
  <p>Thanks</p>