Last active
October 13, 2020 06:39
-
-
Save Abban/6977334 to your computer and use it in GitHub Desktop.
jQuery AJAX file uploads. Code for the following blog post: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax
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
<?php // You need to add server side validation and better error handling here | |
$data = array(); | |
if(isset($_GET['files'])) | |
{ | |
$error = false; | |
$files = array(); | |
$uploaddir = './uploads/'; | |
foreach($_FILES as $file) | |
{ | |
if(move_uploaded_file($file['tmp_name'], $uploaddir .basename($file['name']))) | |
{ | |
$files[] = $uploaddir .$file['name']; | |
} | |
else | |
{ | |
$error = true; | |
} | |
} | |
$data = ($error) ? array('error' => 'There was an error uploading your files') : array('files' => $files); | |
} | |
else | |
{ | |
$data = array('success' => 'Form was submitted', 'formData' => $_POST); | |
} | |
echo json_encode($data); | |
?> |
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
// Variable to store your files | |
var files; | |
// Add events | |
$('input[type=file]').on('change', prepareUpload); | |
// Grab the files and set them to our variable | |
function prepareUpload(event) | |
{ | |
files = event.target.files; | |
} |
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
function submitForm(event, data) | |
{ | |
// Create a jQuery object from the form | |
$form = $(event.target); | |
// Serialize the form data | |
var formData = $form.serialize(); | |
// You should sterilise the file names | |
$.each(data.files, function(key, value) | |
{ | |
formData = formData + '&filenames[]=' + value; | |
}); | |
$.ajax({ | |
url: 'submit.php', | |
type: 'POST', | |
data: formData, | |
cache: false, | |
dataType: 'json', | |
success: function(data, textStatus, jqXHR) | |
{ | |
if(typeof data.error === 'undefined') | |
{ | |
// Success so call function to process the form | |
console.log('SUCCESS: ' + data.success); | |
} | |
else | |
{ | |
// Handle errors here | |
console.log('ERRORS: ' + data.error); | |
} | |
}, | |
error: function(jqXHR, textStatus, errorThrown) | |
{ | |
// Handle errors here | |
console.log('ERRORS: ' + textStatus); | |
}, | |
complete: function() | |
{ | |
// STOP LOADING SPINNER | |
} | |
}); | |
} |
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
$('form').on('submit', uploadFiles); | |
// Catch the form submit and upload the files | |
function uploadFiles(event) | |
{ | |
event.stopPropagation(); // Stop stuff happening | |
event.preventDefault(); // Totally stop stuff happening | |
// START A LOADING SPINNER HERE | |
// Create a formdata object and add the files | |
var data = new FormData(); | |
$.each(files, function(key, value) | |
{ | |
data.append(key, value); | |
}); | |
$.ajax({ | |
url: 'submit.php?files', | |
type: 'POST', | |
data: data, | |
cache: false, | |
dataType: 'json', | |
processData: false, // Don't process the files | |
contentType: false, // Set content type to false as jQuery will tell the server its a query string request | |
success: function(data, textStatus, jqXHR) | |
{ | |
if(typeof data.error === 'undefined') | |
{ | |
// Success so call function to process the form | |
submitForm(event, data); | |
} | |
else | |
{ | |
// Handle errors here | |
console.log('ERRORS: ' + data.error); | |
} | |
}, | |
error: function(jqXHR, textStatus, errorThrown) | |
{ | |
// Handle errors here | |
console.log('ERRORS: ' + textStatus); | |
// STOP LOADING SPINNER | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there, great article and code sample!
I think I have found an error, if you would like to update your gist.
On handleUpload.php, could you update line 5 from:
to: