title: Reading content to the memory from the multi-part file upload
body: <p>I have a problem reading uploaded XML file to the string instead of file.</p>
  
  <p>My problem is that when I try to access the stream (<code>var stream = part.ContentReadStream</code>) then it's closed. I have feeling that it is accessing closed file stream. Am I using MultipartFormDataStreamProvider incorrectly? File size is only few kilobytes, so that should not be a problem.</p>
  
  <pre><code>    [WebInvoke(Method = "POST", UriTemplate = "{importFile}")]
      public HttpResponseMessage Upload(string importFile, HttpRequestMessage request)
      {
          if (!request.Content.IsMimeMultipartContent())
          {
              throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
          }
  
          // Create a stream provider for setting up output streams
          MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider();
  
          // Read the MIME multipart content using the stream provider we just created.
          var task = request.Content.ReadAsMultipartAsync(streamProvider);
          task.Wait();
          IEnumerable&lt;HttpContent&gt; bodyparts = task.Result;
          string submitter;
          if (!bodyparts.TryGetFormFieldValue("submitter", out submitter))
          {
              submitter = "unknown";
          }
  
          // Get list of local file names from stream provider
          IDictionary&lt;string, string&gt; bodyPartFileNames = streamProvider.BodyPartFileNames;
  
  
          var parser = this.parserFactoryFactory.CreateParser();
          foreach (var part in bodyparts)
          {
              using (var stream = part.ContentReadStream)
              {
                  using (var streamReader = new StreamReader(stream))
                  {
                      string content = streamReader.ReadToEnd();
                      var results = parser.Parse(content);
                  }
              }
          }
          return new HttpResponseMessage(HttpStatusCode.Accepted);
      }  
  </code></pre>
  
  <p>This is my post</p>
  
  <pre><code>&lt;h3&gt;Data import test&lt;/h3&gt;
  
  &lt;form action="/api/data/Upload" enctype="multipart/form-data" method="POST"&gt;
      &lt;input type="file" name="importFile"/&gt;
      &lt;input type="submit" value="Upload"/&gt;
  &lt;/form&gt;
  </code></pre>