Skip to content

Instantly share code, notes, and snippets.

@pwnz22
Created October 29, 2016 15:56
Show Gist options
  • Select an option

  • Save pwnz22/c3fc7c81151ad15e6a9d919ddb126cdd to your computer and use it in GitHub Desktop.

Select an option

Save pwnz22/c3fc7c81151ad15e6a9d919ddb126cdd to your computer and use it in GitHub Desktop.
jQuery File Upload
var fileInput = $('input[type=file]'),
button = $('#upload');
button.on('click', function(){
// Access the files property, which holds
// an array with the selected files
var files = fileInput.prop('files');
// No file was chosen!
if(files.length == 0) {
alert('Please choose a file to upload!');
return false;
}
// Create a new FormData object
var fd = new FormData();
fd.append('file', files[0]);
// Upload the file to assets/php/upload.php, which only prints the filename
$.ajax({
url: './assets/php/upload.php',
data: fd,
contentType:false,
processData:false,
type:'POST',
success: function(m){
console.log(m);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment