I have an ‘Upload’ method and I want to display a spinner while data is processing. I’m using EPPlus ‘IFormfile’ to read the data from the input. I have no issue sending the InputFile to my upload method and i wonderin for alternative option where I can pass the ‘InputFile’ via ajax and pass the upload method as parameter instead using the form method"post" or any better approach on this?
var formData = new FormData();
formData.append('inputFile', $('.custom-file-input')[0].files[0]);
var _url = '@Url.Action("Upload", "Exposure")';
debugger;
$.ajax({
url: _url,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (result) {
},
error: function (jqXHR) {
},
complete: function (jqXHR, status) {
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form enctype="multipart/form-data" method="post" asp-controller="Exposure" asp-action="Upload">
<div class="custom-file">
<input type="file" class="custom-file-input" name="InputFile" />
</div>
<button type="submit" id="upload" name="Submit" class="btn btn-info">Upload</button>
<div id="load" style="display:none">
<img src="~/custom/loading.gif" width="500" height="500">
</div>
</form>
<–Method–>
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Upload(IFormFile InputFile)
{
}
2
Answers
Add this code in your _Layout.cshtml file
Add this code in your body tag in _Layout.cshtml
Add this code in your css file which is calling in _Layout.cshtml
beforeSend
might solve your problem. It accepts a function so that you can toggle the loader while handling your request.