【问题标题】:Add more data to IFormFile MVC向 IFormFile MVC 添加更多数据
【发布时间】:2020-05-28 15:25:17
【问题描述】:

如何向 .net core 中的 IFormFile 字段添加更多详细信息

例如,我需要将特定属性附加到

提交文件时如何读取属性 myproperty,因为我使用的是在服务器端捕获文件

包含文件名和输入名称的IFormFile

【问题讨论】:

标签: asp.net-core iformfile


【解决方案1】:

请务必使用asp.net core 3.x。由于github issue,asp.net core 2.2 无法接收到服务器端Model中的IFormFile。

型号:

public class FileInfo
{
    public string FileName { get; set; }
    public string InputName { get; set; }
    public IFormFile File { get; set; }
}

查看:

@model FileInfo

<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="FileName" class="control-label"></label>
                <input asp-for="FileName" class="form-control" />
                <span asp-validation-for="FileName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="InputName" class="control-label"></label>
                <input asp-for="InputName" class="form-control" />
                <span asp-validation-for="InputName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="File" class="control-label"></label>
                <input asp-for="File" value="Upload">
                <span asp-validation-for="File" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

控制器:

public class FileInfoController : Controller
{
    public IActionResult Create()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(FileInfo fileInfo)
    {
        if (ModelState.IsValid)
        {
            //save file info..
        }
        return View(fileInfo);
    }
}

更新:

对于 asp.net core 2.2,如果您的 IFormFile 在嵌套模型中,如下所示的工作演示:

型号:

public class FileInfo
{
    public int Id { get; set; }
    public string FileName { get; set; }
    public string InputName { get; set; }
    public IFormFile File { get; set; }
}
public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
    public FileInfo FileInfo { get; set; }
}

查看:

@model Test
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.FileName" class="control-label"></label>
                <input asp-for="FileInfo.FileName" class="form-control" />
                <span asp-validation-for="FileInfo.FileName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.InputName" class="control-label"></label>
                <input asp-for="FileInfo.InputName" class="form-control" />
                <span asp-validation-for="FileInfo.InputName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FileInfo.File" class="control-label"></label>
                <input asp-for="FileInfo.File" value="Upload">
                <span asp-validation-for="FileInfo.File" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

控制器:

 [HttpPost]
 public async Task<IActionResult> Create(Test test) 
 {
     if (ModelState.IsValid)
     {
         //save file info..
     }
     return View(test);
 }

结果:

【讨论】:

  • 作为包装器,不使用 .net core 3。我没有直接在模型中使用,而是在 Wrapper 类中使用它
  • 你的 asp.net core 版本是什么?我试过 asp.net core 2.2。它可以很好地与嵌套模型一起工作。请检查。
  • 如果对你有帮助,你能接受吗?参考:How to accept as answer。如果还有问题,请跟进让我知道。
猜你喜欢
  • 2014-12-15
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 2017-07-09
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多