I am using this code to send the information of a input
type file
to my data server
let formData = new FormData();
let files = document.querySelector('[type=file]').files;
formData.append('files[]', files[0]);
fetch(url, {
method: 'POST',
body: formData
}).then(response => {
console.log(response);
});
I have reviewed the fetch documentation for POST requests, and also the specific documentation for the body property . These indicate that the body can be any of these types of structures (ArrayBuffer, ArrayBufferView, (Uint8Array and friends), Blob/File, string, URLSearchParams, FormData) but it does not say how to combine them.
So my question is, how do I send a FormData and a string at the same time?
Given a FormData object
form
, the methodappend
(which you're already using) is used to add fields and values:In your backend you will be able to read it the same way you read the file, using the name you have given it. It's similar to using a map .