Home > other >  React Upload File Typescript FormData parameter
React Upload File Typescript FormData parameter

Time:03-25

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!!

CodePudding user response:

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);
}
  • Related