Не удается отправить файл из jquery на контроллер в dot net core 5

#jquery #.net #.net-core

Вопрос:

попробуйте отправить изображение просмотра из jquery на контроллер, но получите сообщение об ошибке. Пожалуйста, помогите мне найти решение.попробуйте много чего, но не можете решить проблему. поэтому, пожалуйста, скажите мне, как отправить файл изображения из jquery на контроллер, и мое свойство файла-IFormFile.

Моя Модель:

 public class UserAddViewModel  {  public string UserName { get; set; }  public IFormFile UserProfilePhoto { get; set; }  }  

Мой HTML:

 lt;input type="file" id="imageUpload" asp-for="UserProfilePhoto" accept=".png, .jpg, .jpeg" placeholder="Browse amp;amp; Upload Image"gt; lt;label for="imageUpload"gt;Browse amp;amp; Upload Imagelt;/labelgt; lt;button id="btnSave" class="blue"gt;Addlt;/buttongt;  

Мой Jquery:

 $(document).ready(function () {   $("#btnSave").click(function () {  var files = $("#imageUpload").get(0).files;  var userAddViewModel = {  UserName: "Adam",  UserProfilePhoto: files[0]   };     $.ajax({  type: "POST",  url: "/User/Create/",  data: JSON.stringify(userAddViewModel),  contentType: "application/json;charset=utf-8",  dataType: "json",  success: function (result) {  // console.log(result);  if (!result.success) {  alert(result.errors);   }  else {  alert(result.errors);    }   },  error: function (errormessage) {  console.log(errormessage.responseText);  alert(errormessage.responseText);   }  });  });   });  

Мой Контроллер:

 public async Tasklt;JsonResultgt; Create([FromBody] UserAddViewModel userAddViewModel)  {  try  {  if (ModelState.IsValid)  {  string[] responseMessage = { "Success" };  return Json(new  {   success = true,  errors = responseMessage  });  }  }  catch (Exception ex)  {  _logger.LogError(ex, "Error in User Creation");  }    return Json(new  {  success = false,  errors = (from item in ModelState  where item.Value.Errors.Any()  select item.Value.Errors[0].ErrorMessage).ToArray()  });  }  

Но Получаю Ошибку:

 System.NotSupportedException: Deserialization of interface types is not supported. Type 'Microsoft.AspNetCore.Http.IFormFile'. Path: $.UserProfilePhoto | LineNumber: 0 | BytePositionInLine: 44.  ---gt; System.NotSupportedException: Deserialization of interface types is not supported. Type 'Microsoft.AspNetCore.Http.IFormFile'.  

Комментарии:

1. Пожалуйста, поделитесь с комментарием содержанием Path: $.UserProfilePhoto | LineNumber: 0 | BytePositionInLine: 44 (или отметьте его в опубликованном коде)