Search code examples
reactjstypescriptaxiosform-data

React Upload File Typescript FormData parameter


I tried to upload a file to my API using React + Typescript, but I can't get the FormData parameters right.

My states defined as this (I use observable here): enter image description here

And here is my upload action:

enter image description here

Thanks!!


Solution

  • The FormData.append() method accepts...

    a USVString or Blob (including subclasses such as File)

    Your PipelineStore has a nullable FileList in its file property. Either add files by index or iterate the list

    //add by index
    formData.append("asda", this.file![0]);
    
    // or iterate
    for (const f of this.file!) {
      formData.append("asda", f);
    }