使用jQuery上传文件:空的FormData值

时间:2018-10-04 16:12:34

标签: jquery ajax

Hello Stackoverflow。

我目前有一个问题,我找不到问题所在。我之前使用AJAX创建了文件上传器,但没有一个出现以下问题。文件输入的值保持为空。我用两种方法尝试过。

首先,我用formData(this)提交了整个表格。这部分起作用。我的其他输入已发送到处理文件的脚本。但是,文件输入不是。即使采用相同的格式,也不会随其提交。

现在我正尝试如下;

  $("#form").on('submit',(function(e) {
        e.preventDefault();

        var formData = new FormData();

        formData.append('customer', document.getElementById("customer").value);
        formData.append('owner', document.getElementById("owner").value);
        formData.append('uploaded_file', document.getElementById("upload").files[0]);

        $.ajax({
            url: "/api/upload",
            type: "POST",
            data:  formData,
            contentType: false,
            cache: false,
            processData:false,
            success: function(data)
            {
                var rtrn = jQuery.parseJSON(data);
                if(rtrn.return = "false")
                {
                    useGGA.createAlert(rtrn.message, rtrn.return, 7500);
                }
                else
                {
                    useGGA.createAlert(rtrn.message, rtrn.return, 7500);
                    $("#form")[0].reset();
                }
            },
            error: function()
            {
                useGGA.createAlert("Something went wrong!", "false", 7500);
            }
        });
    }));

但是。在这里,我有同样的问题。 #upload保持为空。这是我的HTML;

 <form id="form" action="api/upload" method="post" enctype="multipart/form-data">    
      <input type="text" name="owner" id="owner" value="<?= $this->model->data['owner'] ?>" readonly>
      <input type="text" name="customer" id="customer" value="<?= $this->werkstatt->data['ad_id'] ?>" readonly>    
      <input type="file" name="uploaded_file" id="upload">
      <input type="submit" class="__submit_green __launch_upload" value="Ausführen">
</form>

当我现在在控制台中运行以下命令时; document.getElementById("upload").files[0],它返回我Cannot read property 'files' of null。提交表单时也会发生同样的情况。

我在这里俯瞰什么?

0 个答案:

没有答案
相关问题