0

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?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Christian
  • 1,022
  • 13
  • 28
  • https://stackoverflow.com/questions/41367602/upload-files-and-json-in-asp-net-core-web-api – Jeremy Lakeman May 12 '22 at 05:33
  • Maybe you can try define an IFormFile type property in model class, then pass data through `FormData` instead of passing it in json data.You can read [this](https://stackoverflow.com/questions/68222731/is-it-impossible-to-mix-fileupload-and-raw-json-in-asp-core-web-api) to know more. – Qing Guo May 12 '22 at 08:57

2 Answers2

0

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.

Christian
  • 1,022
  • 13
  • 28
0

The best way is, send file inside your model

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

enter image description here

Ali.Ahmadi
  • 51
  • 6