我使用 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 代码片段示例:
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 上,我根本无法输入成功回调并且选择了一个文件。如果没有选择一个文件,它就可以输入它。
另外值得注意的是:文件上传成功!我只是从不输入回调!有任何想法吗?谢谢