title: ASP.NET WebAPI change file name?
body: <p>I am trying to change the file name of images to the value that I posted in the input box <code>username</code>. The files are getting uploaded to the server and also, after overriding <code>GetLocalFileName</code> the file name is changed from "BodyPart_(xyz)" to the original one. How do I rename them to the value that I provided in the input box? </p>
  
  <pre><code>&lt;form name="form1" method="post" enctype="multipart/form-data" action="api/poster/postformdata"&gt;
              &lt;div class="row-fluid fileform"&gt;
                  &lt;div class="span3"&gt;&lt;strong&gt;Username:&lt;/strong&gt;&lt;/div&gt;
                  &lt;input name="username" value="test" type="text" readonly/&gt;
              &lt;/div&gt;
  
              &lt;div class="row-fluid fileform"&gt;
                  &lt;div class="span3"&gt;&lt;strong&gt;Poster:&lt;/strong&gt;&lt;/div&gt;
                  &lt;div class="span4"&gt;&lt;input name="posterFileName" ng-model="posterFileName" type="file" /&gt;&lt;/div&gt;
              &lt;/div&gt;
  
              &lt;div class="row-fluid fileform"&gt;
                   &lt;div class="span8"&gt;&lt;input type="submit" value="Submit" class="btn btn-small btn-primary submitform" /&gt;&lt;/div&gt;
              &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I have stored the value that I received in the <code>newName</code> variable but I am confused on how to rename the file in the server.</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; PostFormData()
              {
                  if (!Request.Content.IsMimeMultipartContent())
                  {
                      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                  }
  
                  string root = HttpContext.Current.Server.MapPath("~/App_Data");
                  var provider = new MultipartFormDataStreamProvider(root);
  
                  try
                  {
                      await Request.Content.ReadAsMultipartAsync(provider);
                      // Show all the key-value pairs.
                      foreach (var key in provider.FormData.AllKeys)
                      {
                          foreach (var val in provider.FormData.GetValues(key))
                          {
                              Trace.WriteLine(string.Format("{0}: {1}", key, val));
                              newName = val;
                          }
                      }
  
                      return Request.CreateResponse(HttpStatusCode.OK);
                  }
                  catch (System.Exception e)
                  {
                      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                  }
              }
  
              public class MyMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
              {
                  public MyMultipartFormDataStreamProvider(string path)
                      : base(path)
                  {
  
                  }
  
                  public override string GetLocalFileName(System.Net.Http.Headers.HttpContentHeaders headers)
                  {
                      string fileName;
                      if (!string.IsNullOrWhiteSpace(headers.ContentDisposition.FileName))
                      {
                          fileName = headers.ContentDisposition.FileName;
                      }
                      else
                      {
                          fileName = Guid.NewGuid().ToString() + ".data";
                      }
                      return fileName.Replace("\"", string.Empty);
                  }
              }
  </code></pre>