I need to upload file in MVC Core via Ajax post request. My Index.cshtml
<input id="addLogoFile" type="file" class="form-control" accept="image/png" />
<input id="addLogoText" type="text" class="form-control" placeholder="Logo title" />
<button type="button" id="addLogoBtn" class="btn btn-sm btn-success"><span class="glyphicon glyphicon-repeat"></span></button>
$("#addLogoBtn").click(function (e) {
e.preventDefault();
var fileInput = $("#addLogoFile");
var formData = new FormData();
var fileTitle = $("#addLogoText").val();
var file = fileInput[0].files[0];
formData.append(file.name, file);
var request = $.ajax({
url: '@Url.Action("AddLogo", "OrganizationManagement")',
type: "POST",
cache: false,
processData: false,
data: { image: formData, imageName: file.name, title: fileTitle },
datatype: "json"
})
request.done(function (data) {
var logos = $("#Logos");
logos.empty();
$.each(data, function () {
logos.append($("<option />").val(this.Value).text(this.Text));
})
})
request.fail(function (e) {
$("#addLogoText").val(e.responseText);
})
})
My ExampleController.cs:
[HttpPost]
public async Task<JsonResult> AddLogo(IFormFile image, string imageName, string title)
{
//code doesn't matter, null in image, imageName and title
}
The problem, is that image, imageName, title are always null. How should I fix it?
EDIT
If I add contentType: false
i have this error in my Network Debugger: The server encountered an unexpected condition that prevented it from fulfilling the request