Прошло много времени с тех пор, как я искал в Интернете решение своей проблемы, но не могу. При выполнении POST свойство типа IFormFile заполняется значением NULL.
Я пытался использовать [FromForm] в своем действии, но это не сработало. Как мне это решить? Спасибо:)
Просмотр модели
public class SistemaBackupViewModel
{
[MaxLength(255, ErrorMessage = "O campo {0} deve ter no máximo {1} caracteres")]
[DisplayName("Nome do Arquivo")]
public string NomeArquivo { get; set; }
public IFormFile UpdateArquivo { get; set; }
}
Действие контроллера
[HttpPost]
[Authorize(Policy = "CanWriteSistemaBackupData")]
[Route("sistema-backup-gerenciar/backup-restore")]
[ValidateAntiForgeryToken]
public IActionResult BackupRestore([FromForm] SistemaBackupViewModel sistemaBackupViewModel)
{
if (!ModelState.IsValid) return View(_sistemaBackupAppService.UpdateAttributesCreateBackup(sistemaBackupViewModel));
if (sistemaBackupViewModel.UpdateArquivo != null)
{
var uniqueFileName = GetUniqueFileName(sistemaBackupViewModel.UpdateArquivo.FileName);
var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
var filePath = Path.Combine(uploads, uniqueFileName);
sistemaBackupViewModel.UpdateArquivo.CopyTo(new FileStream(filePath, FileMode.Create));
//to do : Save uniqueFileName to your db table
}
_sistemaBackupAppService.CreateBackup(sistemaBackupViewModel);
if (IsValidOperation())
ViewBag.Sucesso = "Backup Restaurado!";
return View(_sistemaBackupAppService.UpdateAttributesCreateBackup(sistemaBackupViewModel));
}
View
@model Retaguarda.Application.ViewModels.Sistema.SistemaBackup.SistemaBackupViewModel
@{
ViewData["Title"] = "Restaurar Backup do Sistema";
Layout = null;
}
<div>
<form method="post" asp-action="BackupRestore" asp-controller="SistemaBackup" id="frmBackupRestore" enctype="multipart/form-data" >
@Html.AntiForgeryToken()
<div class="modal-shadow">
<div class="modal-header modal-header-primary">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4><i class="modal-title text-center icon glyphicon glyphicon-export"></i> @ViewData["Title"] </h4>
</div>
<div id="alert-dicas" role="alert" class="alert alert-warning alert-icon alert-dismissible">
<button id="btn-close-dicas" aria-label="Close" class="close" type="button">
<span aria-hidden="true">×</span>
</button>
<i class="icon fa-lightbulb-o" aria-hidden="true"></i>
<h4>Dica</h4>
<p>
Restaurar um backup apagará todos os seus registros e regravará novas informações. Este processo é irreversível!<br />
</p>
</div>
</div>
<div class="panel">
<div class="panel-body container-fluid pt-10 pl-15 pr-15">
<div class="form-horizontal">
<vc:summary />
<div class="form-group row">
@Html.HiddenFor(model => model.NomeArquivo)
<div class="col-md-5">
@*<input type="file" asp-for="UpdateFile" id="input-file-events" class="dropify-event" />*@
<input name="UpdateArquivo" id="UpdateArquivo" type="file" class="form-control" />
</div>
</div>
</div>
</div>
</div>
<div class="col-md-offset-2 col-md-10">
<div class="modal-footer">
<div class="float-left">
<div class="btn-group btn-group-sm mr-auto"
role="group">
<button id="btnIniciarRestauracao" type="submit" class="btn btn-success"><i class="icon wb-check"></i> Iniciar Restauração </button>
<button id="btn-activate-dicas" class="btn btn-dark"><i class="icon fa-lightbulb-o"></i> Mostrar Dicas </button>
<button id="btnFechar" class="btn btn-danger" data-dismiss="modal"><i class="icon wb-close"></i> Fechar </button>
</div>
</div>
</div>
</div>
</form>
</div>
<script>
$('#frmBackupRestore').on('submit', function (e) {
e.preventDefault();
startLoadOneMoment();
});
</script>