1

自从我在网上搜索解决我的问题的方法以来已经有很长时间了,但我做不到。执行 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));
}

看法

    @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>
4

0 回答 0