【问题标题】:jQuery ajaxForm never enters success with file upload on jQuery1.5jQuery ajaxForm 在 jQuery1.5 上的文件上传永远不会成功
【发布时间】:2011-04-18 14:12:33
【问题描述】:

我使用 jquery 表单插件上传文件并在回调中获取 HTML(或文本或任何内容)。

这在 1.5 之前工作得很好,但是一旦我转换到 1.5,只有在表单中选择了一个文件时,回调才会发生。如果不是,回调就会发生并且我的代码会正确触发。这很奇怪也很具体,因为在 1.4 中从未发生过这种情况,我认真地在控制台记录和调试了每一行代码。

这里是示例 JS 代码:

var options= {
        dataType:'html',beforeSubmit:function() {
            $(field).val(filePath);
            loaderdisplay("show");
            $("#reuploadDocumentDialogForm").hide();
        },
        url:actionurl, // the url containing the below function
        type:"POST",
        success:function(responseText, statusText)
        {
                    // If $_FILES was empty, the last IF fires. If not, NOTHING happens.
            console.log(responseText);
            console.log(statusText);
            if (responseText=='success-1')
            {
                loaderdisplay("hide");
                reportStatus(1, "Successfully reuploaded file.");
                $("#reuploadDocumentDialogForm").css("display","inline");
                $("#reuploadDocumentDialog").dialog('close');
            }
            else if (responseText=='success-0')
            {
                loaderdisplay("hide");
                reportStatus(0, "There was an error.File was not uploaded.");
                $("#reuploadDocumentDialogForm").css("display","inline");
            }
            else if (responseText=='error uploading file')
            {
                loaderdisplay("hide");
                reportStatus(0, "File was not uploaded.Try to make the file size smaller.");
                $("#reuploadDocumentDialogForm").show();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
                        // THIS NEVER EVER HAPPENS regardless of what I do
                        alert(textStatus+" - There was an error submitting the form: "+errorThrown);
        }

    };
    $('#reuploadDocumentDialogForm').ajaxForm(options);

这里是 PHP 代码 sn-p 示例:

public function reuploaddocumentAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        if (!empty($_FILES))
        {
            $tempFile = $_FILES['reuploadDocumentDialogFormFile']['tmp_name'];

            $targetFile = $this->_getParam("reuploadDocumentDialogFormTargetFile");
            $result = move_uploaded_file($tempFile,$targetFile);
            die('success-'.$result);
        }
        else
        {
            die('error uploading file');
        }
    }

我试过返回 die(json_encode(array("success" => $result)));以及(并将表单选项中的数据类型更改为 JSON,我尝试将数据类型更改为文本,并将骰子保留为字符串。没有任何效果 - 如果我在 jQuery1.5 上,我根本无法输入成功回调并且选择了一个文件。如果没有选择一个文件,它就可以输入它。

另外值得注意的是:文件上传成功!我只是从不输入回调! 有任何想法吗?谢谢

【问题讨论】:

    标签: php jquery ajax jquery-1.5 jquery-forms-plugin


    【解决方案1】:

    好吧,找到了罪魁祸首。 似乎他们主页上托管的 jquery.form.js 是一个过时的版本。这个确切问题的修复是在我在他们的 github 上找到的最后一个版本(2.6.9)中进行的。在我将我的 jquery 切换到 1.5 并将 jquery 表单切换到 2.6.9 之后,一切都像以前一样工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2013-11-20
      • 1970-01-01
      相关资源
      最近更新 更多