I have used kendo upload to upload files using Ajax Post in MVC core. I can call the API url but data is coming null. Have i passed data correctly? Here is my code.
<form id="uploadForm" action='@Url.Action("UploadFile", new { RmclientAccid = "1" , AsAtDate = "testdate"})' method="post">
@(Html.Kendo().Upload()
.HtmlAttributes(new { aria_label = "files" })
.Name("fileUpload")
)
<p style="padding-top: 1em; text-align: right">
<button type="submit" class="k-button k-primary">Submit</button>
</p>
</form>
$(function () {
$("#uploadForm").submit(function (e) {
e.preventDefault();
var upload = $("#fileUpload").data("kendoUpload"),
files = upload.getFiles(),
myFile = files[0];
console.log(files);
$.ajax({
type: 'POST',
url: '/RmReportTasks/UploadFile/',
dataType: 'json',
processData: false,
contentType: false,
data: {
fileUpload: files[0].rawFile,
RmclientAccid: "1",
AsAtDate: "testdate"
},
success: function (data) {
console.log("succcess")
},
error: function (err) {
console.log("failure");
}
});
});
});
Here is my controller.
[HttpPost]
public async Task<JsonResult> UploadFile(IEnumerable<IFormFile> fileUpload , string RmclientAccid, string AsAtDate)
{
var result = await _fileUploadManager.UploadDocument(fileUpload, RmclientAccid, AsAtDate);
return Json(result);
}
2
Answers
You can not send files directly using
ajax
to the server.And on the server-side, you can receive the file like this.
You can do it without form tag element.
Your
CSHTML
Code should be:Your AJAX Call should be::
Your Controller Action Should be: