I have an action in my ASP.NET Core MVC controller:
[HttpPost]
public async Task<IActionResult> ImportDeviceAsAdmin(UserUploadDeviceVM userUploadDeviceVM)
{
var uploadResult = await this.userService.UploadFilesAsync(userUploadDeviceVM.UserId, userUploadDeviceVM.Files);
return uploadResult ? (IActionResult) Ok("success") : BadRequest("error");
}
and I have a piece of jQuery code:
$(document).ready(function () {
$("input[type='submit']").click(function () {
importUserFiles();
});
});
function importUserFiles() {
const formData = new FormData();
const userId = $("#user-emails-dropdown option:selected").val();
formData.append("UserId", userId);
const fileInput = $("#file-input")[0];
for (let i = 0; i < fileInput.files.length; i++) {
formData.append("Files", fileInput.files[i]);
}
importFileAjax(formData,
function(response) { console.log(response); },
function(response) { console.log(response); });
}
function importFileAjax(formData, successCallback, errorCallback) {
$.ajax({
url: "/Admin/ImportDeviceAsAdmin",
data: formData,
processData: false,
contentType: false,
type: "POST",
success: successCallback,
error: errorCallback
});
}
After I click a Submit button, my model goes to the specified action and the action works with it absolutely perfectly, no problem with it. But my jQuery AJAX call somehow hits neither success
nor error
statements. How can I fix it without specifying async: false
? I have been trying all kinds of promises, async/await etc. from all the Internet but still couldn't fixed the error.
Part of my view with the Submit button:
<div id="file-uploader-container">
<form method="post" enctype="multipart/form-data">
<div class="dropdown-wrapper">
<label for="user-emails-dropdown">Select user</label>
<div>
<select id="user-emails-dropdown" asp-items="ViewBag.Emails" asp-for="UserId">
<!-- there are user emails displayed-->
</select>
</div>
</div>
<div class="file-upload-button-container">
<input id="file-input" name="Files" type="file" multiple />
</div>
<div class="file-upload-button-container">
<input class="btn btn-primary" type="submit" value="Upload" />
</div>
</form>
</div>