可以试试这个它会工作。演示链接
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Ajax file upload</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" target="workFrame" >
<input type="file" name="file" />
<input type="submit" />
</form>
<iframe id="workFrame" src="about:blank" style="display:none;"></iframe>
<div id="image_disp"></div>
</body>
</html>
<script src="http://code.jquery.com/jquery-1.8.1.min.js" type="text/javascript"></script>
<script>
$('form').on('submit', function () {
//check if the form submission is valid, if so just let it submit
//otherwise you could call `return false;` to stop the submission
});
$('#workFrame').on('load', function () {
$('#image_disp').html('');
//get the response from the server
var response = $(this).contents().find('body').html();
$('#image_disp').html('<img src="images/'+response+'"/>');
//you can now access the server response in the `response` variable
//this is the same as the success callback for a jQuery AJAX request
});
上传.php
<?php
if(isset($_FILES['file']) && $_FILES['file'] != '')
{
if(isset($_FILES['file']['name']) && $_FILES['file']['name'] != '')
{
$image_name = explode(".",$_FILES['file']['name']);
$image_type = array_pop($image_name);
move_uploaded_file($_FILES['file']['tmp_name'],'images/upload.'.$image_type);
echo 'upload.'.$image_type;
}
}
?>