, , . , - . FileList, File. . ( .)
, AJAX . FileList . on('submit'), submit. $.ajax(), .
:
<!DOCTYPE html>
<html>
<head lang="en">
<title>Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<style type="text/css">
ul{
width: 650px;
}
ul li {
list-style: none;
width: 165px;
float:left;
position: relative;
margin: 0px 0px 0px 20px;
}
.thumb {
width: 150px;
}
.remove {
position: absolute;
top: 0px;
right: 0px;
color: red;
cursor: pointer;
}
pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
.string { color: green; }
.number { color: darkorange; }
.boolean { color: blue; }
.null { color: magenta; }
.key { color: red; }
</style>
<script type="text/javascript">
$(document).ready(function() {
function jsonPrettify(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}
var filesArray = [];
function previewFiles( e ) {
var files = e.target.files;
var preview = $('#imagePreviews');
for (var i = 0, f; f = files[i]; i++) {
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
reader.onload = (function(theFile) {
return function(e) {
var li = $('<li><img class="thumb" src="'+ e.target.result +'" title="'+ escape(theFile.name) +'"/><div class="remove">X</div></li>');
preview.append(li);
filesArray.push(theFile);
};
})(f,preview);
reader.readAsDataURL(f);
}
}
$('#fileInput').on('change', previewFiles);
function removeFile( index ){
filesArray.splice(index, 1);
}
$(document).on('click', '#imagePreviews .remove',function(e){
var image = $(this).closest('li');
console.log(image.index());
console.log('Files:' ,filesArray);
removeFile(image.index());
console.log('Files:' ,filesArray);
image.fadeOut(function(){
$(this).remove();
});
});
function submitForm(e){
e.preventDefault();
var formData = new FormData($('#myForm')[0]);
for(var i= 0, file; file = filesArray[i]; i++){
formData.append('files[]', file);
}
$.ajax({
url: 'upload.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
$('#response').html(jsonPrettify(response));
}
});
}
$('#myForm').on('submit', submitForm);
});
</script>
</head>
<body>
<div style="width: 650px; border: 1px #D8D8D8;">
<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
<label>Name</label>
<input type="text" name="firstName" value="FirstName"/>
<input type="text" name="lastName" value="LastName"/>
<input id="fileInput" type="file" value="Upload button" multiple/>
<div style="width: 600px; border: 1px solid #D8D8D8;">
<ul style="list-style: none;" id="imagePreviews">
</ul>
<div style="clear: both;"> </div>
</div>
<br>
<input type="submit"/>
</form>
<br>
Upload.php response<br>
<pre id="response" style="width: 650px;"></pre>
</div>
</body>
</html>
<?php
header('Content-Type: application/json');
echo json_encode(array('success' => true, '$_FILES' => $_FILES, '$_POST' => $_POST));
?>
, .