title: ASP.NET Web API upload image to SQL Server database
body: <p>I'm writing an article API with image and I've been following this <a href="http://code-adda.com/2018/05/upload-file-on-server-using-asp-net-web-api/" rel="nofollow noreferrer">tutorial</a> about uploading file in ASP.NET web API, the <code>Title</code> and <code>Content</code> are saved to database as intended.</p>
  
  <p>The problem here is, the images that I post are saved to my local folder but the fields <code>Filename</code>, <code>Filepath</code>, <code>Filelength</code>, and <code>Filecreatedtime</code> aren't saved to database.</p>
  
  <p>Posting article to database with postman:</p>
  
  <p><a href="https://i.stack.imgur.com/CosDb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CosDb.png" alt="posting article to database"></a></p>
  
  <p>The <code>ImageFolder</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/QsNoo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QsNoo.png" alt="imageFolder"></a></p>
  
  <p><code>GET</code> index:</p>
  
  <p><a href="https://i.stack.imgur.com/7FaOa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7FaOa.png" alt="get index"></a></p>
  
  <p>The database:</p>
  
  <p><a href="https://i.stack.imgur.com/Wh7Bj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wh7Bj.png" alt="article database"></a></p>
  
  <p>here's my <code>Article</code> model:</p>
  
  <pre><code>namespace Wwf_Article.Models
  {
      using System;
      using System.Collections.Generic;
  
      public partial class Article
      {
          public int ID { get; set; }
          public string Title { get; set; }
          public string Content { get; set; }
          public string FileName { get; set; }
          public string FilePath { get; set; }
          public float FileLength { get; set; }
          public DateTime FileCreatedTime { get; 
      }
  }
  </code></pre>
  
  <p>and here's my <strong>POST controller</strong></p>
  
  <pre><code>[Mime]
  public async Task&lt;IHttpActionResult&gt; Post()
  {
  
          var fileuploadPath = HttpContext.Current.Server.MapPath("~/ImageFolder");
  
          var multiFormDataStreamProvider = new MultiFileUploadProvider(fileuploadPath);
  
          await Request.Content.ReadAsMultipartAsync(multiFormDataStreamProvider);
  
          string uploadingFileName = multiFormDataStreamProvider.FileData.Select(x =&gt; x.LocalFileName).FirstOrDefault();
  
          Article article = new Article
          {
              Title = HttpContext.Current.Request.Form["Title"],
              Content = HttpContext.Current.Request.Form["Content"],
  
              //these four lines below aren't saved to DB 
              FilePath = uploadingFileName,
              FileName = Path.GetFileName(uploadingFileName),
              FileLength = new FileInfo(uploadingFileName).Length,
              FileCreatedTime = DateTime.Now
          };
  
          db.Articles.Add(article);
          db.SaveChanges();
  
          return Ok();
  }
  </code></pre>
  
  <p>Any idea how to fix this problem?</p>