0

How I can add more details to IFormFile field in .net core

for example i need to attach specific property to

How I can read the property myproperty when submit the file since i use to catch the file in server side

IFormFile which contains the filename and the name of input

AYKHO
  • 516
  • 1
  • 7
  • 20
  • Maybe is duplicated in [asp-net-core-web-api-file-upload-and-form-data-multiple-parameter-passing-to-method](https://stackoverflow.com/questions/51892706/asp-net-core-web-api-file-upload-and-form-data-multiple-parameter-passing-to-m) – nwpie May 24 '20 at 16:52
  • No it's not duplicate here submition data separate than IFormFile Object my issue similar to this https://github.com/dotnet/aspnetcore/issues/22187 – AYKHO May 24 '20 at 16:54

1 Answers1

1

Be sure that you use asp.net core 3.x.Due to the github issue,asp.net core 2.2 could not receive the IFormFile which in the Model in the server side.

Model:

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

View:

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

Controller:

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);
    }
}

UPDATE:

For asp.net core 2.2,if your IFormFile is in a nested model,a working demo like below:

Model:

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; }
}

View:

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

Controller:

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

Result: enter image description here

Rena
  • 30,832
  • 6
  • 37
  • 72
  • as a wrapper for this without using .net core 3. I used not directly in the model i used it in Wrapper class – AYKHO May 27 '20 at 02:09
  • What is your version of asp.net core?I have tried asp.net core 2.2.It could work well with nested model.Please check. – Rena May 28 '20 at 07:26