You can access the files from another button and then post them using ajax as normal. ButtonFileUpload8 is the ID for the new button, file-8 is the ID for the file upload control. You have to disable the upload button
This is not perfect code, I just put it together to show how its done.
HTML:
<form enctype="multipart/form-data">
<div class="row mb-2">
<div class="col-md-8">
<div class="file-loading">
<input id="file-8" type="file" multiple>
</div>
</div>
</div>
<div class="btn-group btn-group-sm mr-1" role="group">
<button id="ButtonFileUpload8" type="button" class="btn btn-sm">
Blue
</button>
</div>
</form>
Javascript (note this ignores the post URL set in the initiator so calls FileUploadKrajee instead and uses ajax):
$("#file-8").fileinput({
uploadUrl:"@Url.Action("FileUploadKrajee", "App")",
uploadAsync: true,
minFileCount: 1,
maxFileCount: 5,
overwriteInitial: false,
initialPreview: "",
initialPreviewConfig: "",
uploadExtraData: "",
showUpload: false
});
// Click of upload button
$("#ButtonFileUpload8").click(function () {
var control = document.getElementById("file-8");
var files = control.files;
var formData = new FormData();
for (var i = 0; i != files.length; i++) {
formData.append("files", files[i]);
}
$.ajax({
url: 'UploadFiles3',
type: "POST",
contentType: false, // Do not set any content header
processData: false, // Do not process data
data: formData,
async: false,
success: function (result) {
if (result != "") {
alert(result);
}
control.files.value("");
},
error: function (err) {
alert(err.statusText);
}
});
});
Then in the receiving controller you can use:
public ActionResult UploadFiles3(List<IFormFile> files)
{
string nams = "";
string funame = "";
foreach (IFormFile source in files)
{
string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.ToString();
filename = this.EnsureCorrectFilename(filename);
nams = nams + source.FileName.ToString();
funame = "D:\\Data\\PointsAlign\\Core\\" + filename;
FileStream output = System.IO.File.Create(funame);
source.CopyTo(output);
}
return Json("Hi, Alster. Your files uploaded successfully " + nams);
}
This does work but I haven't stress tested it yet for any issues