This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
postFile('http://example.com/api/v1/users', 'input[type="file"].avatar') | |
.then(data => console.log(data)) | |
function postFile(url, fileSelector) { | |
const formData = new FormData() | |
const fileField = document.querySelector(fileSelector) | |
formData.append('username', 'abc123') | |
formData.append('avatar', fileField.files[0]) | |
return fetch(url, { | |
method: 'POST', // 'GET', 'PUT', 'DELETE', etc. | |
body: formData // Coordinate the body type with 'Content-Type' | |
}) | |
.then(response => response.json()) | |
.catch(error => console.error(error)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment