-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
34 lines (33 loc) · 1.61 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const form = document.querySelector("#my_form");
const book_upload = document.querySelector("#book_upload");
const results = document.querySelector("#results");
form.addEventListener("submit", e => {
/* preventDefault, so that the page doesn't refresh */
e.preventDefault();
/* you can fill the formData object automatically with all the data from the form */
const formData = new FormData(form);
/* or you can can instantiate an empty FormData object and then fill it using append(). The three arguments to append are the key (equivalent to the name field on an input), the file itself, and an optional third argument for the filename. */
const formData2 = new FormData();
formData2.append(
"csv_file",
book_upload.files[0],
book_upload.files[0].name
);
/* You can iterate through the FormData object to view its data (this is equivalent to using the .entries() method.) */
for (const item of formData2) {
results.innerHTML = `
<p><strong>name:</strong> ${item[0]}</p>
<p><strong>filename:</strong> ${item[1].name}</p>
<p><strong>size:</strong> ${item[1].size}</p>
<p><strong>type:</strong> ${item[1].type}</p>
`;
}
/* once you've confirmed that the FormData object has all the proper data, send a fetch request. This particular request will go nowhere since I never defined the API_ROOT variable */
fetch('http://localhost:4000/api/v1/books/batch_create', {
method: "POST",
body: formData,
headers: new Headers({
'Authorization': 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjozMSwiZXhwIjoxNTYyNzU2NTQ1fQ.waYlp3KJpBY51FFbjHY0h3RiHMorV2g_P3p_3RnJA7U',
}),
});
});