简体   繁体   English

将带有 ajax 的图像发送到 php 文件

[英]Send image with ajax to php file

I need to send an image via ajax to a php file.我需要通过 ajax 将图像发送到 php 文件。 I can not use form tags for my purposes.我不能将表单标签用于我的目的。 Based on what I've read, the best way to go about this is set processData and contentType to false and dataType to json.根据我所读到的,关于此问题的 go 的最佳方法是将 processData 和 contentType 设置为 false 并将 dataType 设置为 json。 If I click submit, ajax doesn't appear to fire.如果我点击提交,ajax 似乎不会触发。 If I delete "dataType: 'json'", ajax fires and I get an alert "getimagesize([object FormData]): failed to open stream: No such file or directory in...".如果我删除“dataType: 'json'”,ajax 会触发并且我收到警告“getimagesize([object FormData]): failed to open stream: No such file or directory in...”。 I've read that I may need to reset Content-type on the server end, but I'm not sure how to go about this.我读过我可能需要在服务器端重置 Content-type ,但我不确定如何 go 解决这个问题。

index.php 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>

uploadFile.php上传文件.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";
}

?>

On the PHP page you must get the data by data not image because that's the name you have set for the data in ajax在 PHP 页面上,您必须通过data而不是图像获取数据,因为这是您为 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";
}
?>

And yes, you are posting data to sendFile.php which is wrong you should post it to uploadFile.php是的,您将数据发布到sendFile.php这是错误的,您应该将其发布到uploadFile.php

Add your code as following:添加您的代码如下:

JAVASCRIPT 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 PHP

var_dump($_FILES['image'});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM