Good day everyone,
I'm trying to upload file using ajax from client side to server side (asp.net core) controller but I'm having a null value.
Here's my html and javascript codes:
<input type="file" id="myfile" class="required" />
<button type="button" class="btn btn-info" onclick="uploadcsvfile()">
<script>
function uploadcsvfile() {
var myfile= document.getElementById("myfile");
var formData = new FormData();
if (myfile.files.length > 0) {
for (var i = 0; i < myfile.files.length; i++) {
formData.append('file-' + i, myfile.files[i]);
}
}
$.ajax({
url: "/MyController/UploadFile/",
type: "POST",
dataType: "json",
data: formData,
contentType: false,
processData: false,
success: function(data){
},
error: function (data) {
}
})
}
</script>
Here's my controller that uses IFormFile
public async Task<JsonResult> UploadFile(IFormFile formData)
{
// do something here
}
Thank you in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…