title: Why does the FileName of the ContentDisposition add extra slashes and quotes + Exception thrown?
body: <p><a href="https://i.stack.imgur.com/pWIaO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pWIaO.png" alt="enter image description here"></a></p>
  
  <p>I'm trying to get files uploaded with ASP.NET WebApi2. I'm trying out two options: the first is to have the file in memory. For that I use a piece of code that is an extension on the HttpContent class:</p>
  
  <pre><code>public static async Task&lt;HttpPostedData&gt; ParseMultipartAsync(this HttpContent postedContent)
          {
              var provider = await postedContent.ReadAsMultipartAsync();
  
              var files = new Dictionary&lt;string, HttpPostedFile&gt;(StringComparer.InvariantCultureIgnoreCase);
              var fields = new Dictionary&lt;string, HttpPostedField&gt;(StringComparer.InvariantCultureIgnoreCase);
  
              string[] toegestaneExtensies = { ".jpg", ".jpeg", ".gif", ".png", ".pdf", ".txt", ".xlsx" };
  
              foreach (var content in provider.Contents)
              {
                  var fieldName = content.Headers.ContentDisposition.Name.Trim('"');
  
                  if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))
                  {
                      bool geuploadBestandHeeftToegestaneExtensie = (toegestaneExtensies.Contains(Path.GetExtension(content.Headers.ContentDisposition.FileName)));
  
                      if ((content.Headers.ContentLength &gt; 0) &amp;&amp; geuploadBestandHeeftToegestaneExtensie)
                      {
                          var file = await content.ReadAsByteArrayAsync();
                          var fileName = content.Headers.ContentDisposition.FileName.Trim('"');
                          files.Add(fieldName, new HttpPostedFile(fieldName, fileName, file));
                      }
                  }
                  else
                  {
                      if (fieldName != "submit")
                      {
                          var data = await content.ReadAsStringAsync();
                          fields.Add(fieldName, new HttpPostedField(fieldName, data));
                      }
                  }
              }
  
              return new HttpPostedData(fields, files);
          }
      }
  </code></pre>
  
  <p>This throws an exception though, because FileName is not in the right format. I can see the filename being "normal" in the picture, but looking into ContentDisposition, FileName is suddenly "\"banana.jpg\"". Why is this?</p>
  
  <p>The second thing I'm trying is to do is to save to disk. This seems to work, I can write the file to disk, but get the following error message:</p>
  
  <p>"error writing MIME multipart body part to output stream" - "Access denied".</p>
  
  <p>It is simply the following code:</p>
  
  <pre><code>public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
  {
      public CustomMultipartFormDataStreamProvider(string path) : base(path)
      {
      }
  
      public override string GetLocalFileName(HttpContentHeaders headers)
      {
          return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
      }
  }
  
  var streamProvider = new MultipartFormDataStreamProvider("d:/");
  var t = await Request.Content.ReadAsMultipartAsync(streamProvider);
  </code></pre>
  
  <p>But no access is denied, the file is written, it wasn't there before, the file has a normal filename. What could be going on?</p>