Skip to content

Instantly share code, notes, and snippets.

@wnstn
Created February 11, 2019 21:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnstn/bdc98f1946246f2705be34b8bf1e8f87 to your computer and use it in GitHub Desktop.
Save wnstn/bdc98f1946246f2705be34b8bf1e8f87 to your computer and use it in GitHub Desktop.
const uploadInput = document.querySelector('input[type="file"]');
const awsFormData = JSON.parse(uploadEl.getAttribute('data-form-data'));
const formData = new FormData();
Object.keys(awsFormData).forEach((key)=>{
formData.append(key, awsFormData[key]);
});
uploadInput.addEventListener('change', function(ev){
ev.preventDefault();
const file = ev.target.files[0];
formData.append('Content-Type', file.type);
formData.append('file', file);
const config = {
method: "POST",
headers: new Headers({
"Accept": "application/xml"
}),
body: formData,
};
return fetch(url, config)
.then(response => response.text())
.then((xml) => {
// decode xml and handle response
})
.catch((e) => console.error.bind(console))
})
@yann-yinn
Copy link

thanks, was struggling with Content-Type :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment