首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向IFormFile MVC添加更多数据

向IFormFile MVC添加更多数据
EN

Stack Overflow用户
提问于 2020-05-24 01:09:36
回答 1查看 756关注 0票数 0

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

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

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

IFormFile,它包含文件名和输入名称。

EN

回答 1

Stack Overflow用户

发布于 2020-05-25 03:30:20

请确保您将asp.net核心3.x.Due用于github问题,asp.net核心2.2无法接收服务器端模型中的IFormFile。

型号:

代码语言:javascript
复制
public class FileInfo
{
    public string FileName { get; set; }
    public string InputName { get; set; }
    public IFormFile File { get; set; }
}

查看:

代码语言:javascript
复制
@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>

主计长:

代码语言:javascript
复制
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核心2.2,如果您的IFormFile位于嵌套模型中,则如下所示:

型号:

代码语言:javascript
复制
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; }
}

查看:

代码语言:javascript
复制
@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>

主计长:

代码语言:javascript
复制
 [HttpPost]
 public async Task<IActionResult> Create(Test test) 
 {
     if (ModelState.IsValid)
     {
         //save file info..
     }
     return View(test);
 }

结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61980455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档