2

I want to upload multiple files and used IFormFile to upload file using asp.net core 5 but the problem is it shows me string data type instead of the IFormFile. Can I just override the field attachment: "string" property to File object? See details below:

Sample json data

id: 1122,
name: "yourname",
address: "your complete address",
attachments: [{
  "Id": 0,
  "Name": "string",
  "attachment": "string" // file attachment, I expect to be form upload button or similar
}] // please do note that this will have maximum of 5 arrays
// can attachment: string json property can be override with File object?

Main Model

public class MainModel
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    [MaxLength(10)]
    public List<Attachment> Attachments { get; set; }
}

Attachment Model

public class Attachment
{
   [Required]
   public int Id { get; set; }
   [Required]
   public string Name { get; set; }
   [Required]
   [DataType(DataType.Upload)]
   public IFormFile Attachment { get; set; } // this is weird, instead of upload file it shows string instead.
}

Main Controller

[HttpPut("main/update")]
public IActionResult UpdateMain([FromForm] MainModel model)
{
   if(ModelState.IsValid)
   {
      var result = _test.UpdateMain(model);
      return Ok(new { data = result });
   }
   return BadRequest();
}

I don't know why it has a string DataType into and it doesn't make sense for me.

enter image description here

The data being sent to the web api is a File object which it doesn't make sense. Can someone help out in this one?

5
  • What are Id and Name in your Attachments I mean where they come from when you have not uploaded anything? Commented May 20, 2021 at 10:12
  • its a sample data inside the class model inside itself, Commented May 20, 2021 at 10:15
  • 1
    What is the issue? 1] In Database, it shows string type instead of a blob. 2] from react side, it passes the file but here in aspnet, it does not show? Commented May 20, 2021 at 10:16
  • @Nilay thats exactly my question and also how to pass data into it? can I just override it by passing a File object? Commented May 20, 2021 at 10:19
  • You are using IFormFile in Nested object which is a known bug and not going to fixed! So You need to flow this answer. stackoverflow.com/a/56352648/2251733 Commented May 20, 2021 at 10:24

2 Answers 2

5

Use IList<IFormFile> in your attachment. So your model would be like this:

public class MainModel
{
    [Required]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public IList<IFormFile> Attachments { get; set; }
}

In this way Attachments property contains all the selected files in your client you have chosen. Over each item of Attachments you have its FileName and some other useful data like FileSize, ...

Sign up to request clarification or add additional context in comments.

1 Comment

yeah I think there are no option for now, I think I go with this route and create a separate list of attachment details and map it through Zip Linq function. Anyway, thanks guys!
0

The following works for in net 6 for me, instead of model we can get files from HttpContext too:

    [HttpPost("Upload")]
    public ActionResult Upload()
    {
        var files = HttpContext.Request.Form.Files;
        if (files != null && files.Count > 0)
        {
            //processing / upload
        }
        return Ok();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.