0

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

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

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

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

4

1 回答 1

1

请确保您使用的是asp.net core 3.x。由于github问题,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);
 }

结果: 在此处输入图像描述

于 2020-05-25T03:30:20.680 回答