Created
October 29, 2016 15:56
-
-
Save pwnz22/c3fc7c81151ad15e6a9d919ddb126cdd to your computer and use it in GitHub Desktop.
jQuery File Upload
This file contains hidden or 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
| 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