taha suliman

taha suliman

  • 1.3k
  • 364
  • 5.7k

Problem uploading files via asp.net core api

Nov 29 2020 6:02 AM
Why can I upload files via the Postman application, but when I use Microsot, I cannot upload where is the problem with the following code?
Model is:
  1. public class Book    
  2. {    
  3.         [Key]    
  4.         public int BookId { get; set; }    
  5.         [Display(Name = "Book name")]    
  6.         [Required(ErrorMessage = "Data feild is required")]    
  7.         public string BookName { get; set; }    
  8.         [Display(Name ="Book details")]    
  9.         [Required(ErrorMessage = "Data feild is required")]    
  10.         public string BookDetails { get; set; }    
  11.         [Display(Name = "Publish date")]    
  12.         [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]    
  13.         [Required(ErrorMessage = "Data feild is required")]    
  14.         public DateTime BookPdate { get; set; }    
  15.         [Display(Name = "Book name")]    
  16.         [Required(ErrorMessage = "Data feild is required")]    
  17.         [ForeignKey("Author")]    
  18.         public int AuthortId { get; set; }    
  19.         public virtual Author Author { get; set; }    
  20.         public string ImagePath { get; set; }    
  21.         [NotMapped]    
  22.         public IFormFile ImageFile { get; set; }    
  23. }   
Client controller method:
  1. [HttpPost]    
  2. public IActionResult AddOrEdit(Book book)    
  3. {    
  4.     if (book.BookId == 0)    
  5.     {    
  6.         HttpResponseMessage resp = GlobalApiClient.webApiClient.PostAsJsonAsync("Books", book).Result;    
  7.         TempData["SuccessMsg"] = "Saved Successfully ... ";    
  8.         return RedirectToAction("Index");    
  9.     }    
  10.     else    
  11.     {    
  12.         HttpResponseMessage resp = GlobalApiClient.webApiClient.PutAsJsonAsync("Books/" + book.BookId , book).Result;    
  13.         if (resp.IsSuccessStatusCode)    
  14.         {    
  15.             TempData["SuccessMsg"] = "Updated Successfully ... ";    
  16.         }    
  17.         else    
  18.         {    
  19.             TempData["SuccessMsg"] = "Updated not complete ... ";    
  20.         }    
  21.         return RedirectToAction("Index");    
  22.     }    
  23. }
Api controller method:
  1. //// PUT api/<controller>/5    
  2. [HttpPut("{id}")]    
  3. public async Task<IActionResult> Put(int id,[FromBody] Book book)    
  4. {    
  5.     if (ModelState.IsValid)    
  6.     {    
  7.         try    
  8.         {    
  9.             if (id == book.BookId)    
  10.             {  
  11.                 string fileName = book.ImagePath;    
  12.                 if (book.ImageFile.Length > 0)    
  13.                 {    
  14.                     string path = hosting.WebRootPath + "\\Uploads\\";    
  15.                     if (!Directory.Exists(path))    
  16.                    {    
  17.                         Directory.CreateDirectory(path);    
  18.                     }    
  19.                     using (FileStream fileStream = System.IO.File.Create(path + book.ImageFile.FileName))    
  20.                     {    
  21.                        book.ImageFile.CopyTo(fileStream);    
  22.                        fileStream.Flush();    
  23.                    }    
  24.                 }    
  25.                 book.ImagePath = fileName;    
  26.                 await bookRepo.Update(book);    
  27.                 return Ok();    
  28.             }    
  29.             return NotFound();    
  30.         }    
  31.         catch (Exception)    
  32.         {    
  33.             return BadRequest();    
  34.         }    
  35.     }    
  36.     return BadRequest();    
  37. }

Answers (3)