I am uploading files to my web APi (in c#, ASP. NET) using ajax and a html file selector. I have a viewmodel and I want to pass the file to my controller, using my viewmodel.
This is my code: My viewmodel
public class CursoViewModel
{
public Guid CursoId { get; set; }
[MaxLength(125)]
public string Titulo { get; set; }
public string Descripcion { get; set; }
[Required(ErrorMessage ="select image file for course")]
[DataType(DataType.Upload)]
public HttpPostedFileBase Imagen { get; set; } //note this is the image
}
My view,
<form id="Editcurso" method="post" action="#" enctype="multipart/form-data">
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "Please fix the following errors.")
<div class="container">
<div class="form-group">
@Html.LabelFor(c=>c.Titulo)
@Html.TextBoxFor(c => c.Titulo, new { @class="form-control"})
@Html.ValidationMessageFor(m => m.Titulo)
</div>
<div class="form-group">
@Html.LabelFor(c => c.Descripcion)
@Html.TextAreaFor(c => c.Descripcion, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Descripcion)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Imagen)
@Html.TextBoxFor(m => m.Imagen, new {type = "file"})
@Html.ValidationMessageFor(m => m.Imagen)
</div>
<button id="submiter" type="submit" class="btn btn-primary">Listo!</button>
</div>
My javascript:
$('#Editcurso').submit(function(e) {
e.preventDefault(); // prevent the default submit
if (!$(this).valid()) {
return; // exit the function and display the errors
}
jQuery.support.cors = true;
var data = new FormData();
var files = $("#Imagen").get(0).files;
// Add the uploaded image content to the form data collection
if (files.length > 0) {
data.append("UploadedImage", files[0]);
console.log("files uploaded");
}
var viewmodel = new Object();
viewmodel.Titulo = "Sourav";
viewmodel.Descripcion = "Kayal";
viewmodel.CursoId = "some guid";
viewmodel.Imagen = files; /*<<==I pass the file here*/
$.ajax({
url: '/api/ProfesorCurso/test',
type: 'PUT',
dataType: 'json',
data: viewmodel,
success: function (data) {
console.log(data);
return false;
},
error: function (x, y, z) {
alert('error al postear');
return false;
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
My api
[HttpPut]
public IHttpActionResult UpdateCursoProfesor([FromBody] CursoViewModel viewmodel)
{
if (!ModelState.IsValid) return BadRequest();
try
{
//do something, upload the file inside the viewmodel
return Ok(result);
}
catch (DbEntityValidationException e)
{ //do something}
When I execute my program, I get this error message: "Uncaught TypeError: Illegal invocation". What am I doing wrong? thanks