weixin_33712881 2016-06-07 23:14 采纳率: 0%
浏览 7

使用Ajax上传文件

I have tried several variations of this (based on articles and other SO forums) to get this to work and I have narrowed it down to the fact that the data is getting lost when I try to send the form data to my PHP script. When I echo $_FILES["file"]["name"] it is empty.

I have tested and my ajax and php scripts are connected as I can return some data, just not any form data. This is obviously resulting in the files not being uploaded.

My current code is in reference to this forum (lee8oi's answer): jQuery Ajax File Upload I started a new forum because this question was asked 6 years ago.

html:

<form id="signUpForm" name="signUpForm"  action="../functions/newUser.php" method="post">
    <input type="file" class="su_photo" name="file" id="user-photo">
    <input type="submit" id="sign-up" type="button" class="btn btn-primary btn-sm pull-right" value="Get Started">
</form>

js:

  $('#signUpForm').on('submit',function(){
        var fd = new FormData( $('#signUpForm') );
        fd.append("label", "WEBUPLOAD");
        $.ajax({
          url: "../../functions/newUser.php",
          type: "POST",
          data: fd,
          processData: false,  // tell jQuery not to process the data
          contentType: false   // tell jQuery not to set contentType
        }).done(function( data ) {
            console.log("PHP Output:");
            console.log( data );
        });
        return false;
    });

php:

   if ($_POST["label"]) {
      $label = $_POST["label"];
    }
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        } else {
            $filename = $label.$_FILES["file"]["name"];
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

            if (file_exists("uploads/" . $filename)) {
                echo $filename . " already exists. ";
            } else {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "uploads/" . $filename);
                echo "Stored in: " . "uploads/" . $filename;
            }
        }
    } else {
        echo $_FILES["file"]["name"];
    }
  • 写回答

1条回答 默认 最新

  • 7*4 2016-06-07 23:50
    关注

    I think you just need to change the following line

    var fd = new FormData( $('#signUpForm') );
    

    to

    var fd = new FormData( $('#signUpForm')[0] );
    

    or

    var fd = new FormData(this);
    

    The constructor of FormData expects a native form element, not a jQuery object. So document.getElementById('signUpForm'), $('#signUpForm')[0], and this would all work.

    This answer provides further explanation.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀