开发者

jQuery ajaxForm never enters success with file upload on jQuery1.5

开发者 https://www.devze.com 2023-02-26 04:48 出处:网络
I use a jquery form plugin to upload a file and get HTML (or text or anything at all) in the callback.

I use a jquery form plugin to upload a file and get HTML (or text or anything at all) in the callback.

This worked fine until 1.5, but as soon as I converted to 1.5, the callback never happens ONLY IF a file was selected in the form. If it was not, the callback happens and my code fires properly. This is very odd and very specific, because it never happened with 1.4 and I seriously console logged and debugged every line of code.

Here is the sample JS code:

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);

And here is the PHP code snippet example:

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');
        }
    }

I have tried returning die(json_encode(array("success" => $result))); as well (and changed the dataType in the form options to JSON, and I 开发者_如何转开发have tried changing the dataType to text, and leaving the die as a string. Nothing works - I simply cannot enter the success callback if I'm on jQuery1.5 AND a file was selected. It enters it just fine if a file was not selected.

Also worth noting: the file gets uploaded ok! I just never enter callback! Any ideas? Thanks


Well, found the culprit. Seems the jquery.form.js hosted on their homepage is an outdated version. The fix for this exact problem was made in the last build (2.6.9) which I found on their github. After I switched both my jquery to 1.5 and jquery form to 2.6.9., everything worked as it used to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号