简体   繁体   English

Ajax FormData文件上传不起作用[JQuery] [PHP]

[英]Ajax FormData file upload not working [JQuery] [PHP]

My upload form isn't working for some reason, when I submit the form the PHP file runs and my $_FILES variable is empty. 由于某种原因,我的上传表单无法正常工作,当我提交表单时,PHP文件开始运行,并且我的$ _FILES变量为空。 I've been stuck for hours and I feel like I've been through every post on this site and I just don't understand what's going wrong. 我已经被困了几个小时,感觉好像我已经浏览了该站点上的每条帖子,但我只是不明白出了什么问题。

HTML 的HTML

    <form id="uploadform" name="uploadform" method="POST" enctype="multipart/form-data">
      <input id="realupload" name="realupload" type="file" multiple/>
      <input id="uploadsubmit" type="submit" value="upload"/>
</form>

JQuery jQuery查询

$('#uploadsubmit').click(function(e)
{
    e.preventDefault();

        $.ajax({
            type: "POST",
            url: "upload.php",
            data: function(){
                alert("test");
                var data = new FormData();
                data.append("realupload", jQuery("#realupload").get(0).files[0]);
                return data;
            }
            ,
            processData:false,
            contentType: false,
            cache: false,
            success: function(theData){
                $('#innercontent').html(theData);
            },
            error: function() {
                $('#innercontent').html("ERROR, HELP");
            }
        });

    } 
});

PHP (upload.php) PHP(upload.php)

<?php
//upload.php
if (isset($_FILES["realupload"]))
{
    echo "SUCCESSFUL UPLOAD " . $_FILES["realupload"]["error"];
}
else{
    echo "FILES NOT SET";
}
?>
  • The PHP file is just a test file to check if the upload worked PHP文件只是一个测试文件,用于检查上传是否正常
  • FILES NOT SET is output every time 每次输出文件未设置
  • The alert inside of the ajax data function doesn't run, I'm not sure if it's should be Ajax数据功能内部的警报无法运行,我不确定是否应该
  • Trying to create a multiple file uploader 尝试创建多个文件上传器

Could this be to do with the server? 这可能与服务器有关吗? There is already a forum uploaded on there that in itself allows uploads, so I don't think that that is the case, but I'm not sure. 那里已经有一个上传的论坛,它本身允许上传,所以我不认为是这种情况,但是我不确定。

Thanks! 谢谢!

Edit: Guys, thanks for the links, but you can now upload files with HTML5 and the XmlHttpRequest2 object and I'm trying to figure that out. 编辑:伙计们,感谢您的链接,但是您现在可以使用HTML5和XmlHttpRequest2对象上传文件,而我正设法弄清楚这一点。

Just use new jquery library jquery-2.1.1.min.js from http://jquery.com/download/ 只需使用来自http://jquery.com/download/的新jquery库jquery-2.1.1.min.js

It will definitely solve your problem. 它肯定会解决您的问题。 A few days before i was stuck with same problem so I gave try for above library it did work for me. 几天前,我遇到了同样的问题,因此我尝试了上面的库,它确实为我工作。

您无法通过ajax发送文件,则必须直接使用PHP来做到这一点。

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

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