1 HTML
<form id="frmCommodity" enctype="multipart/form-data" class="form-horizontal">
<input id="files" type="file" name="files" multiple />
</form>
2 JS
$("#btnUpdateCommodity").unbind().click(function () {
var fileUpload = $("#files").get(0);
var files = fileUpload.files;
var data = new FormData();
for (var i = 0; i < files.length; i++) {
data.append(files[i].name, files[i]);
}
$.ajax({
url: "/OrderingMeal/UpdateAnnouncement",
data: data,
method: "post",
processData: false,
contentType: false,
success: function (result) {
}
});
});
3 后台C#
public IActionResult UpdateAnnouncement()
{
try
{
var files = HttpContext.Request.Form.Files;
long size = 0;
foreach (var file in files)
{
var filename = ContentDispositionHeaderValue
.Parse(file.ContentDisposition)
.FileName
.Trim('"');
filename = @"D:\文件目录" + $@"\{filename}";
size += file.Length;
using (FileStream fs = System.IO.File.Create(filename))
{
file.CopyTo(fs);
fs.Flush();
}
}
return Json(new { Success = true, Message = "添加成功!" });
}
catch (Exception err)
{
logger.Error(err, "添加失败@");
return Json(new { Success = false, Message = "添加失败:" + err.ToString()
}
}