0

I am trying to take the following form and convert it to use XMLHttp. Cannot get a working example of how to convert this to use XMLHttp.

<form enctype='multipart/form-data' action='process.do'>
  <input id='myfile' type='file'>
  <input type='hidden' name='systemid' value='a1312423r1rde223e423e'>
  <input type='hidden' name='systemname' value='My value'>
</form>

var x = new XMLHttpRequest()
x.open('POST', 'process.do');
// Where to add systemid, systemname parameters?
x.send($('myfile').files[0])
C B
  • 12,482
  • 5
  • 36
  • 48

1 Answers1

1

Just send() a FormData object instantiated from the form object.

var x = new XMLHttpRequest()
x.open('POST', 'process.do');
x.send(new FormData(document.querySelector("form")));

That will include all the data from the form.

You'll need to give your file input a name though.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335