Skip to content

Instantly share code, notes, and snippets.

@Alexzanderk
Last active March 25, 2018 07:56
Show Gist options
  • Save Alexzanderk/62d718f18e8d716123fdb5a69e69d3bf to your computer and use it in GitHub Desktop.
Save Alexzanderk/62d718f18e8d716123fdb5a69e69d3bf to your computer and use it in GitHub Desktop.
send file
var formData = new FormData();
var fileField = document.querySelector("input[type='file']");
formData.append('username', 'abc123');
formData.append('avatar', fileField.files[0]);
fetch('https://example.com/profile/avatar', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', response));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment