我正在尝试通过 ajax 将图像发送到 ajax 处理程序。ìmage如果我console.log我发送的变量,它看起来很好。截至目前,我正在将其发送给处理程序并尝试将其回显。但我得到的答复是空白的。我正在向image: formData内部发送一个我希望没问题的对象。
阿贾克斯:
var form_data = new FormData();
var image = $('#newImage').prop('files')[0];
var image_name = image.name;
var image_extension = image.name.split('.').pop().toLowerCase();
if(jQuery.inArray(image_extension, ['gif', 'jpg', 'JPG', 'pjpeg', 'x-png', 'png', 'PNG', 'jpeg']) == -1) {
alert("Invalid Image File Type")
}
var image_size = image.size;
if(image_size > 700000) {
alert("Image too big!");
} else {
form_data.append('file', image);
}
let data = {
action: 'NewEventExhibition',
name: name,
description: description,
notes: notes,
status: status,
slug: slug,
start_date: start_date,
end_date: end_date,
event_code: '<?=$code?>',
image: form_data
};
$.ajax({
url: '/modules/ajax/ajax_handler.php',
type: 'POST',
data: data,
contentType: false,
cache: false,
processData: false,
mimeType: 'multipart/form-data',
success: function(response) {
alert(response);
},
fail: function(response) {
console.log(response);
}
})
}
});
处理程序
if($_FILES['file']['name'] != '') {
$test = explode(".", $_FILES['file']['name']);
$extension = end($test);
$name = rand(100, 999).'.'.$extension;
$location = "/assets/images/".$name."";
move_uploaded_file($_FILES['file']['tmp_name'], $location);
echo $location;
}
```
慕森王