Skip to content

Instantly share code, notes, and snippets.

@LasseRafn
Created May 18, 2017 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LasseRafn/9323245af2857be003f471f4712b7cef to your computer and use it in GitHub Desktop.
Save LasseRafn/9323245af2857be003f471f4712b7cef to your computer and use it in GitHub Desktop.
function generateFormData() {
// define upload field
let file_input = document.getElementById('profile_picture');
// define form data
let data = new FormData();
// Add regular text field
data.append("name", "John Doe");
// If any files added to file_input, add first
if (file_input.files.length > 0) {
data.append('profile_picture', file_input.files[0]);
}
return data;
}
// Now simply post the generated form data using ajax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment