Home > Software design >  Combine Json and file upload in a controller
Combine Json and file upload in a controller

Time:05-16

I'm trying to send a form with Angular to a controller that contains json and a file.

[HttpPost("Save", Name = "SaveReportRequest")]
public ActionResult<ReportRequestBean> Save([Bind("deviceType,buildType,version,qGateDate,notes")]
            ReportRequestModel reportRequest, IFormFile file)
{
    // Validate reportRequest
    if (ModelState.IsValid == false)
    {
        return BadRequest("Invalid ModelState");
    }

    if (ValidateQGateDate(reportRequest.QGateDate) == false)
    {
        return BadRequest("QGateDate is outside of min or max.");
    }
}

But all I get is

[07:22:31 INF] Request finished HTTP/2 POST https://localhost:5001/api/ReportRequest/Save application/json 130 - 415 175 application/problem json; charset=utf-8 20.0784ms

How can I set the right Content-Type or how do I build it to make it work?

CodePudding user response:

For me the easiest solution was to upload the file one by one and save them in a temp file. Then when I save the Form, I load the temp file and save it.

CodePudding user response:

The best way is, send file inside your model

public class RequestModel
{
 ... (other properties)
 public IFormFile file { get; set; }
}

enter image description here

  • Related