我正在使用Ajax和PHP。
我想在不点击提交按钮的情况下上传图像,我正在使用这个答案中描述的方法:如何使用jquery ajax和php上传文件(不点击任何提交按钮)
但它不起作用。我需要更改数据类型还是其他什么?
这是我的代码:
jQuery/Ajax
$(document).ready(function(){
var b = new FormData($(this).closest("form")[0]);
b.append('image', $('input[type=file]')[0].files[0]);
$("#imgInput").change(function(){
$.ajax({
url: "test.php",
type: "post",
datatype: 'json',
processData: false,
contentType: false,
data: b,
success: function(text){
if(text== "success"){
alert("Image uploaded");
}
},
error: function(){
alert("Not uploaded");
}
});
});
});
test.php
<?php
$filename=$_FILES["image"]["name"];
$filetmp=$_FILES["image"]["tmp_name"];
$filetype=$_FILES["image"]["type"];
$filepath="images/".$filename;
move_uploaded_file($filetmp, $filepath);
?>
HTML
<form name="form1" method="post" enctype="multipart/form-data">
<table>
<tr>
<td><input type="text" name="name" placeholder="Enter your name" /></td>
<td rowspan="3"><div class="propic"><img id="" /></div>
<input id="imgInput" type="file" name="image" /></td>
</tr>
<tr>
<td><input type="text" name="username" placeholder="Enter username"/></td>
</tr>
<tr>
<td><input id="digits" type="text" name="phone" maxlength="10" placeholder="Enter your phone no."/></td>
</tr>
<tr>
<td><input type="password" name="password" maxlength="12" placeholder="Enter password"/></td>
<td><input id="button" type="submit" name="submit" value="Sign Up" /></td>
</tr>
</table>
</form>
您需要使用FormData()
通过AJAX将文件发送到服务器。将脚本更改为:
$.ajax({
url: "test.php",
type: "POST",
data: new FormData($(this).closest("form")[0]), // This is the main place you need.
contentType : false,
async: false,
processData: false,
cache: false,
success: function(text) {
if(text== "success"){
alert("Image uploaded");
}
}
});
如果你只是想让它发挥作用,我推荐Ravishanker Kusuma的Hayageek jQuery File Upload plugin
我通常不推荐插件,但这个插件非常棒。它几乎为你做了所有的工作
http://hayageek.com/docs/jquery-upload-file.php
他将这个过程分解为四个简单的步骤,基本上看起来是这样的:
查找//1//2//3:
<head>
// (1) Load the js files (note that it requires jQuery, which we also load)
<link href="http://hayageek.github.io/jQuery-Upload-File/uploadfile.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://hayageek.github.io/jQuery-Upload-File/jquery.uploadfile.min.js"></script> // (1)
</head>
<body>
<div id="fileuploader">Upload</div> // (2) Create DIV
<script>
$(document).ready(function(){
$("#fileuploader").uploadFile({ // (3) initialize plugin
url:"my_php_processor.php",
fileName:"myfile"
});
});
</script>
</body>
最后(第四步)是创建一个与上面在jQuery代码中指定的名称相同的PHP文件(在本例中为my_php_processor.php
),以接收和处理文件:
my_php_processor.php:
<?php
$output_dir = "uploads/";
$theFile = $_FILES["myfile"]["name"];
move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName);
注意PHP中的myfile
($_FILES["myfile"]
)和jQuery代码块中指定的文件名myfile
之间的关系
在Hayageek网页上,研究Server
选项卡上的upload.php
示例。
请注意,您还可以使用dynamicFormData将其他变量传递到my_php_processor.php
处理器文件。参见其他示例:
$("#fileuploader").uploadFile({
url:"my_php_processor.php",
fileName:"myfile",
dynamicFormData: function(){
return {
//my_php_processor.php will receive POST vars below
newSubj: $("#newSubj").val(),
newBody: $("#newBody").val(),
};
},
onSuccess:function(files,data,xhr,pd){
//files: list of files
//data: response from server
//xhr : jquery xhr object
alert(xhr.responseText); //displays data ECHOd by `my_php_processor.php`
}
});
my_php_processor.php:
<?php
$n = $_POST['newSubj'];
$b = $_POST['newBody'];
$uploadedFile = $_FILES["myfile"]["name"];
//etc.
echo 'This will display in the alert box';
jsFiddle示例代码-单击图像示例