我试图在不使用任何jquery插件的情况下上传文件,这些插件基于How to upload multiple files using PHP, jQuery and AJAX的想法。现在我的多个文件的html部分是这样的。
<form method="post" name="addstudent" id="registrationform" enctype="multipart/form-data">
some input fields
<!--first file-->
<div class="file_div_child">
<input class="file" type="file" name="file[]" style="display: block;">
<button class="remove first_remove">X</button>
</div>
<!--second file-->
<div class="file_div_child">
<input class="file" type="file" name="file[]" style="display: block;">
<button class="remove first_remove">X</button>
</div>
-------so on
--also other input fields in form
<input type="submit" id="buttontext" class="student_registrationform_button" value="submit" />
</form>
我的jquery:
$('#buttontext').click(function(){
formdata = false;
if (window.FormData) {
formdata = new FormData();
}
var i = 0, len = $(".file").length, img, reader, file;
$('.file').each(function() { var file = this.files[0];
if (window.FileReader) {
reader = new FileReader();
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("file", file);
}
});
$.ajax({
url: 'process.php',
type: 'POST',
data:formdata ,
success:function(data){ //alert(data);
console.log(data);return false;
});
在process.php中。我正在检查$_POST['file'] and $_POST['other_inputfields']
。它给我空字符串