Skip to content

Instantly share code, notes, and snippets.

@borsch
Created August 18, 2017 15:05
Show Gist options
  • Save borsch/e34d2b8e2cde034528b2b065b56800fb to your computer and use it in GitHub Desktop.
Save borsch/e34d2b8e2cde034528b2b065b56800fb to your computer and use it in GitHub Desktop.
upload file with data via ajax to server
var payload = new FormData();
payload.append('file', $('#my_file_input')[0].files[0]);
payload.append('some_field', 'foo');
var request = new XMLHttpRequest();
request.open('PUT'|'POST', '/my/api/upload/url');
request.setRequestHeader('Accept', 'application/json');
request.onload = function () {
var response = request.response;
console.log(response);
};
request.send(payload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment