I have a requirement where I want to upload an excel file to the controller, read the file, process its data and send it back to the same view as the JSON object.
I want to achieve this using AJAX call as I want to capture its success callback and manipulate DOM as per received response. I have tried a few things but I am not able to hit the controller. Any help on this is appreciated.
Below shown is my JS, HTML and C# code :
function SubmitInfo() {
var formData = new FormData();
formData.append('file', $('#fileInput')[0].files[0]); // myFile is the input type="file" control
var _url = '@Url.Action("Upload", "CrossValidation")';
$.ajax({
url: _url,
type: 'POST',
data: formData,
processData: true, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (result) {
//manipulate DOM and bind response with Kendo grid
alert("result");
},
error: function (jqXHR) {
},
complete: function (jqXHR, status) {
}
});
}
<div class="col-md-4">
<input id="fileInput" type="file">
</div>
<div class="col-md-4">
<input type="submit" value="Upload file" onclick="SubmitInfo()"/>
</div>
public JsonResult Upload(IFormFile formData)
{
//Do something here....
return Json("");
}