我正在使用jQuery Ajax,我想将文件上传到ajax响应的特定文件夹。
我的HTML表单:
<form type="post" method="post" action="" id="newCustomerForm" enctype="multipart/form-data">
<div>
<label for="username">Username</label>
<input name="username" id="username" type="text" />
</div>
<br />
<div>
<label for="email">Email</label>
<input name="email" id="email" type="text" />
</div>
<br />
<div>
<label for="message">Message</label>
<input name="message" id="message" type="text" />
</div>
<br />
<div>
<label for="file">File</label>
<input name="file" id="file" type="file" />
</div>
<br />
<input type="hidden" name="action" value="addCustomer"/>
<input type="submit" name="submit" value="Submit">
</form>
我的Ajax代码:
<script type="text/javascript">
$(function() {
$("#newCustomerForm").validate({
rules: {
"username": {
required: true
}
},
messages: {
"username": {
required: "Please, enter a name"
}
},
submitHandler: function(form) {
$.ajax({
type: "POST",
url: "<?php echo esc_url( home_url( '/' ) ); ?>wp-admin/admin-ajax.php",
data: $(form).serialize()+'&file='+document.getElementById('file').value,
timeout: 3000,
success: function(data) {
$("#feedback").html(data);
},
error: function() {
alert('failed');
}
});
return false;
}
});
});
</script>
我在bellow函数中获取ajax数据:
add_action('wp_ajax_addCustomer', 'addCustomer');
function addCustomer() {
echo $username = $_POST['username'];
echo "<br />";
echo $email = $_POST['email'];
echo "<br />";
echo $message = $_POST['message'];
echo "<br />";
echo $file = $_POST['file'];
echo "<br />";
die();
}
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');
我在回复时收到文件名,但我很困惑如何将图像上传到项目文件夹中的特定文件夹。
如果您帮助我解决问题,我将不胜感激。
谢谢:)