我正在尝试将文件上传到我的控制器,但是在toUser接受权限的情况下,它将其视为null。
我正在使用System.Web.Http
,所以我不能使用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) {
}
})