I want to upload multiple files from multiple button clicks in asp.net core razor view.
Below is my jquery code which is not working.
<script type="text/javascript" src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#btnupload1').on('click', myFunction('#fileupload1'));
$('#btnupload2').on('click', myFunction('#fileupload2'));
$('#btnupload3').on('click', myFunction('#fileupload3'));
})
function myFunction(value) {
var fdata = new FormData();
var fileUpload = $(value).get(0);
var files = fileUpload.files;
fdata.append(files[0].name, files[0]);
$.ajax({
type: "POST",
url: "/Contract/UpFiles",
beforeSend: function(xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: fdata,
contentType: false,
processData: false,
success: function(response) {
if (response.length == 0)
alert('Some error occured while uploading');
else {
$('#divPrint').html(response);
}
},
error: function(e) {
$('#divPrint').html(e.responseText);
}
});
}
</script>