0

I want to do this very simple thing: post a file to my controller.

I have a .net core 6.0 backend. My frontend API client is generated by NSwag

my controller looks like this: (the model contains an 'IFormFile' called file.)

 [HttpPost("single-file"), DisableRequestSizeLimit]
    public async Task<ActionResult<int>> Create([FromForm] CreateUploadedDocumentModel model)
    {...

this works in the generated swagger UI.

the generated angular client however, is not.

file comes from html input

<input  (change)="selectFiles($event)" class="form-control" type="file" id="formFileMultiple">

Files are of type FileList[];

on submit i loop through the files and call "upload":

    this.uploadClient.create(1, file, file.name).subscribe(
      result => {
        console.log(result);

      },
      err => {
        this.message = 'Could not upload the file:' + file.name;
      });
  }

and here's the auto generated angular web api client call:

  create(consultantId: number | undefined, file: FileParameter | null | undefined, fileName: string | null | undefined): Observable<number> {
        let url_ = this.baseUrl + "/api/UploadedDocuments/single-file";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = new FormData();
        if (consultantId === null || consultantId === undefined)
            throw new Error("The parameter 'consultantId' cannot be null.");
        else
            content_.append("ConsultantId", consultantId.toString());
        if (file !== null && file !== undefined)
            content_.append("File", file.data, file.fileName ? file.fileName : "File");
here -->    if (fileName !== null && fileName !== undefined)
            content_.append("FileName", fileName.toString());

so when i try to post, i get an error: : FormData.append: Argument 2 is not an object. because the file.data is undefined in the auto generated client..

i have tried MANY different things :) i could just create a simple post of formdata, but i need this to work 'out of the box' with the auto generated client from Nswag

any help or pointers will be MUCH appreciated, thanks in advance :)

jps
  • 20,041
  • 15
  • 75
  • 79
Kasper Sølvstrøm
  • 270
  • 1
  • 2
  • 22

1 Answers1

0

if anyone should run into this problem...

i simply had to wrap the file from the form input like so:

let fileParameter: FileParameter = { data: file, fileName: file.name };
Kasper Sølvstrøm
  • 270
  • 1
  • 2
  • 22