Search code examples
javascriptform-data

Change name with formData.set


I have in formData:

Content-Disposition: form-data; name="property-photos"; filename="Screen Shot 2017-12-22 at 10.31.21 AM.png"
Content-Type: image/png.

This is being sent by a multiple file file input.

I have three photos labeled property-photos and would like to use a for loop to change them to PropertyPhoto1, PropertyPhoto2 and PropertyPhoto3

How do I use formData.set to change this?

I would have thought it was something similar to:

formData.set('PropertyPhoto'+(i+1), fileName, fileType).

Solution

  • Set the name when appending the File object. See FormData.append()

    let fd = new FormData();
    for (let i = 0; i < files.length; i++) {
      fd.append("PropertyPhoto" + (i + 1), files[i], "PropertyPhoto" + (i + 1))
    }