您可以从另一个按钮访问文件,然后照常使用 ajax 发布它们。ButtonFileUpload8 是新按钮的 ID,file-8 是文件上传控件的 ID。您必须禁用上传按钮
这不是完美的代码,我只是把它放在一起来展示它是如何完成的。
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(请注意,这忽略了在发起程序中设置的帖子 URL,因此调用 FileUploadKrajee 并使用 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);
}
});
});
然后在接收控制器中,您可以使用:
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);
}
这确实有效,但我还没有针对任何问题对其进行压力测试