title: change file name before upload to server asp.net core
body: <blockquote>
    <p>Hi, I want to change file name before upload to server and store path
    in database . Here is my code. But it uploads file with same name. I
    want to change name (with guid). How can I do that?</p>
  </blockquote>
  
  <pre><code> [HttpPost, ValidateAntiForgeryToken]
          public async Task&lt;IActionResult&gt; SaveAsync(NewPostViewModel model, IFormFile postImage)
          {
  
              var unitOfWork = new UnitOfWork(new NewsDbContext(_contextOptions));
  
             // My domain class which is contains CoverPhotoPath property
              var post = new Post();
  
              if (postImage != null &amp;&amp; postImage.Length &gt; 0)
              {
                  string filename = ContentDispositionHeaderValue.Parse(postImage.ContentDisposition).FileName.Trim('"');
  
                  filename = this.EnsureCorrectFilename(filename);
                  using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename)))
                      await postImage.CopyToAsync(output);
               // this is my photo path in database 
                  post.CoverPhotoPath = filename;
              }
  
  
              unitOfWork.Posts.Add(post);
              unitOfWork.Compelete();
  
              return RedirectToAction("List", "News");
          }
  </code></pre>
  
  <blockquote>
    <p>My methods</p>
  </blockquote>
  
  <pre><code>private string EnsureCorrectFilename(string filename)
          {
              if (filename.Contains("\\"))
                  filename = filename.Substring(filename.LastIndexOf("\\") + 1);
  
              return filename;
          }
  
  
   private string GetPathAndFilename(string filename)
      {
          return _environment.WebRootPath + "\\files\\" + filename;
      }
  </code></pre>