Super lightweight (1Kb) & simple AJAX file upload javascript library.
No html, no iframe, no form needed. Just only one js file is all you need.
<p><input type="button" value="Select files to upload" data="upload" /></p>
<script src="TinyFileUploader/tinyfileuploader-1.0.js"></script>
<script>
var uploader = new TinyFileUploader({
post_url: '/tinyfileuploader.php',
selector: '[data=upload]',
success: function(data) {
alert(JSON.stringify(data));
}
});
</script>
<?php
if(!empty($_FILES)) {
header('Content-Type: application/json');
echo json_encode($_FILES);
die;
}
?>
var uploader = new TinyFileUploader({ post_url: '/upload_files', // url to upload files to multiple: true, // allow multiple files select dialog auto_send: true, // auto start upload after files are selected data: { // any custom data id: 123 }, selector: '.upload_btn', // bind File Upload dialog onclick to this element select: function(data) {}, // file select callback function success: function(data) {}, // upload success callback function error: function(data) {} // upload error callback function });
var uploader = new TinyFileUploader({ post_url: '/upload_files', auto_send: false }); // ... then later uploader.open(); // opens file upload dialog // ... and then uploader.send(); // sends files to the server (to use with auto_send = false)
Source (3Kb): tinyfileuploader-1.0.js
Minified (1Kb): tinyfileuploader-1.0.min.js