我知道不工作是一个模糊的问题描述。在控制台中我看到错误:
TypeError: $(...).files is undefined
以下是用于发送Ajax请求的jQuery代码:
$(document).ready(function(){
$("input[type=submit]").click(function(e){
e.preventDefault();
e.stopPropagation();
$.ajax({
url : "",
type : "POST",
data : $('input[type=file]').files[0],
success : function(){
console.log("Successfully done!");
},
});
});
});
这是HTML:
<form method="POST" enctype="multipart/form-data">
<label >Please Select a File to Upload</label><br />
<input type="file" name="name" /><br /><br />
<input type="submit" value="Upload" name="upload" />
</form>
以下是要上传文件的PHP:
session_start();
if(isset($_POST['upload']))
{
print_r($_FILES);
move_uploaded_file( $_FILES['name']['tmp_name'] , 'up/'.$_FILES['name']['name'] );
echo "File uploaded Successfully";
echo "<br />" . "<br />";
}
上述所有代码只是在一个页面中。
答案 0 :(得分:0)
试试这个,因为AJAX默认提交字符串
中的文件$("form").submit(function(e) {
e.preventDefault();
var fd = new FormData($("form")[0]);
$.ajax({type: 'POST',
url: "",
data: fd,
processData: false,
contentType: false,
success: function() {
// if success
}
});
return false;
});
这个
if(is_uploaded_file($_FILES['name']['tmp_name'])){
move_uploaded_file( $_FILES['name']['tmp_name'] , 'up/'.$_FILES['name']['name'] );
echo "File uploaded Successfully";
echo "<br />" . "<br />";
}