0

I'm trying to upload a file to my controller but its taking it as null, while the toUser is accepting right.

I am using System.Web.Http, so I cant use System.Web.Mvc

MessageController.cs

[Route("api/messages/saveChatAttachment/{toUser}"), HttpPost]
public HttpResponseMessage SaveAttachment(HttpPostedFileBase file, int toUser)
{
    return Request.CreateResponse(HttpStatusCode.OK, toUser);
}

jQuery

var attachment;

$('#chatAttachment').on('change', function (e) {
  if (e.target.files.length) {
    attachment = e.target.files[0];
  }
});

var formData = new FormData();
formData.append('file', attachment);

$.ajax({
  url: '/api/messages/saveChatAttachment/' + toUser,
  type: 'POST',
  data: formData,
  contentType: 'application/json',
  dataType: 'json',
  cache: false,
  processData: false,
  success: function (data) {

  }
})
Ertan Hasani
  • 797
  • 1
  • 18
  • 37

0 Answers0