Skip to content

Instantly share code, notes, and snippets.

@moosetraveller
Created August 17, 2022 15:01
Show Gist options
  • Save moosetraveller/1a137da1ad26efe522f1f608664acc7f to your computer and use it in GitHub Desktop.
Save moosetraveller/1a137da1ad26efe522f1f608664acc7f to your computer and use it in GitHub Desktop.
Upload a File with Fetch API

Upload a File with Fetch API

async function upload() {

    const input = document.querySelector('input[type="file"]');

    const data = new FormData();
    data.append("file", input.files[0]);

    const response = await fetch('/files', {
        method: 'POST',
        body: data
    });

    const responseData = await response.json();

    console.log(responseData);

}

Credits

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