使用ajax将图像发送到php文件

使用ajax将图像发送到php文件,php,jquery,ajax,Php,Jquery,Ajax,我需要通过ajax将图像发送到php文件。我不能将表单标记用于我的目的。根据我所读的内容,最好的方法是将processData和contentType设置为false,将dataType设置为json。如果我单击submit,ajax似乎不会启动。如果我删除“dataType:'json'”,ajax会触发,我会收到一个警报“getimagesize([object FormData]):无法打开流:中没有这样的文件或目录…”。我已经读到,我可能需要重置服务器端的内容类型,但我不知道如何进行 i

我需要通过ajax将图像发送到php文件。我不能将表单标记用于我的目的。根据我所读的内容,最好的方法是将processData和contentType设置为false,将dataType设置为json。如果我单击submit,ajax似乎不会启动。如果我删除“dataType:'json'”,ajax会触发,我会收到一个警报“getimagesize([object FormData]):无法打开流:中没有这样的文件或目录…”。我已经读到,我可能需要重置服务器端的内容类型,但我不知道如何进行

index.php

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).on('click', '#sendFile', function() {   
            var file = $("#file").prop('files')[0];
            var data = new FormData();
            data.append('image', data);
            $.ajax({
                type: "POST",
                processData: false,
                contentType: false,
                dataType: 'json',           
                url: 'uploadFile.php',
                data: data,
                success: function(data){
                    alert(data);
                }
            });
        });
    </script>
</head>
<body>
    <input type="file" name="file" id="file"><br>
    <button id="sendFile">Submit</button>
<body>
</html>

$(document).on('click','#sendFile',function(){
var file=$(“#file”).prop('files')[0];
var data=new FormData();
data.append('image',data);
$.ajax({
类型:“POST”,
processData:false,
contentType:false,
数据类型:“json”,
url:'uploadFile.php',
数据:数据,
成功:功能(数据){
警报(数据);
}
});
});

提交
uploadFile.php

<?php

if(isset($_POST['image'])){
    $data = $_POST['image'];
    echo getImageSize($data); //Just to test if $data is an image file
} else {
    echo "Not working";
}

?>

在PHP页面上,您必须通过
data
而不是image来获取数据,因为这是您在ajax中为数据设置的名称

<?php
if(isset($_POST['data'])){
    $data = $_POST['data'];
    echo getImageSize($data); //Just to test if $data is an image file
} else {
    echo "Not working";
}
?>


是的,您正在将数据发布到
sendFile.php
,这是错误的。您应该将数据发布到
uploadFile.php
添加您的代码,如下所示:

JAVASCRIPT

var file = $("#file")[0].files[0];
        var data = new FormData();
        data.append('image', file);
$.ajax({
            type: "POST",
            processData: false,
            contentType: false,
            cache: false,
            dataType: 'json',           
            url: 'uploadFile.php',
            mimeType: 'multipart/form-data',
            data: data,
            success: function(data){
                alert(data);
            }
        });
PHP


也许在做出改变之后,ajax仍然不会启动。如果我删除了“dataType:'json'”,我会收到“Not working”警报。你能告诉我开发工具中的ajax状态吗?你正在将数据发布到
sendFile.php
这是错误的,你应该将其发布到
uploadFile.php
uploadFile.php。只是改变了。我没有使用过开发工具,所以我正在想办法。好的,别担心,我会告诉你调试ajax请求的基本过程,按Ctrl+Shift+I,然后在网络选项卡中进入
XHR
,然后启动请求,从那里你可以调试ajax调用Sajax没有启动。如果我删除了“dataType:'json'”,我会得到一个警告,上面写着“undefined index:image”,我得到的结果与before@James,我更新了formdata集。现在再试试我的答案。ajax没有开火。然后我删除了“dataType:'json'”,并收到警报“array(5){[“name”]=>string(8)”home.png“[“type”]=>string(9)”image/png“[“tmp_name”]=>string(24)”C:\xampp\tmp\phpF432.tmp”[“error”]=>int(0)[“size”=>int(6259)}不工作”@James。您必须学习php中的文件上传过程。
var_dump($_FILES['image'});