jQuery ajaxForm 在 jQuery1.5 上上传文件时永远不会成功
我使用 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,我根本无法输入成功回调并且选择了一个文件。如果未选择文件,它就可以正常输入
:文件已上传,我只是从未输入回调! 有什么想法吗?谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,罪魁祸首找到了。
似乎他们主页上托管的 jquery.form.js 是一个过时的版本。这个确切问题的修复是在我在他们的 github 上找到的最后一个版本(2.6.9)中进行的。当我将 jquery 切换到 1.5 并将 jquery form 切换到 2.6.9 后,一切都像以前一样工作。
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.