我在后端的自定义区域中创建wordpress插件。 我想用ajax调用创建上传拖放。 我不明白我怎么能在wordpress中进行ajax调用? 我的阿贾克斯电话没有工作。为什么我该怎么做?
这是我的代码。FILE.JS
jQuery(function($) {
.....
// Drop
$('.upload-area').on('drop', function (e) {
e.stopPropagation();
e.preventDefault();
var file = e.originalEvent.dataTransfer.files;
var fd = new FormData();
fd.append('file', file[0]);
uploadData(fd);
});
// Open file selector on div click
$("#uploadfile").click(function(){
$("#file").click();
});
// file selected
$("#file").change(function(){
var fd = new FormData();
var files = $('#file')[0].files[0];
fd.append('file',files);
uploadData(fd);
});
});
function uploadData(formdata){
jQuery.ajax({
url: action.php,
type: 'post',
data: formdata,
contentType: false,
processData: false,
dataType: 'json',
success: function(response){
window.location.reload();
}
});
}
action.php的
$filename = $_FILES['file']['name'];
$namedef = explode("-",$filename);
$filesize = $_FILES['file']['size'];
$location = "media/".$filename;
$return_arr = array();
if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
if(is_array(getimagesize($location))){
$src = $location;
}
$return_arr = array("name" => $filename,"size" => $filesize, "src"=> $src);
}
echo json_encode($return_arr);
的index.php
<input type="file" name="file" id="file">
<div class="upload-area" id="uploadfile">
</div>