jQuery的文件上传隐藏的iframe [英] Jquery File Upload Hidden IFrame
问题描述
我需要就如何得到一个AJAX风格的文件上传使用隐藏的iframe去的简单说明。下面是的HTML code有关的形式部分:
I needed a quick explanation on how to get an AJAX style file upload going using a hidden iframe. Here's the portion of the HTML code pertaining to the form:
<div id = "file" class = "info">
<form id="file_upload_form" method="post" enctype="multipart/form-data" action='script/uploadScript'>
<input name="file" id="file" size="27" type="file" /><br />
<input id = "uploadSubmit" type="submit" name="action" value="Upload" />
<iframe id="upload_target" name="upload_target" src="" style="width:0;height:0;border:0px solid #fff;"></iframe>
</form>
</div>
下面是有关PHP:
public function uploadScript(){
$returnVals = array();
if ($_FILES["file"]["error"] > 0){
$returnVals['error'] = "Error: " . $_FILES["file"]["error"] . "<br />";
}else{
if ($_FILES["file"]["type"] != "text/plain"){
$returnVals['error'] = "Badtype";
}else{
$returnVals['text'] = file_get_contents($_FILES["file"]["tmp_name"]);
}
}
echo json_encode($returnVals);
}
因此,从本质上讲,PHP获取文件并检查它是否是一个文本文件。然后,我想在JavaScript访问返回的JSON。这就是我感到困惑......
So, essentially, the PHP gets the file and checks if it's a text file. Then, I want to access the returned json within javascript. That's where I get confused...
$("#file_upload_form").submit(function() {
$("#file_upload_form").target = "upload_target";
$("#upload_target").onload = uploadDone();
});
在理想情况下,当上载完成且iframe中加载,这应该叫
Ideally, when the upload is complete and the iframe is loaded, this should call
function uploadDone() {
alert($("#upload_target").contents().find("body").text());
}
不过,这总是空白。本质上,returnVals放入的IFRAME体,但警报之后,因此警报无关中它。那么,有没有办法解决这个问题有点瑕疵?
But, that's always blank. Essentially, the returnVals are placed into the iframe body, but after the alert, so the alert has nothing in it. So, is there a way to remedy this little flaw?
是否有关于如何做到这一点任何更好的解释?我一直在敲打我的头用不同的样品codeS等了一整天,没有运气:/
Is there any better explanation on how to do this? I've been banging my head against it with different sample codes and etc all day with no luck :/
我基本上是希望能够提醒通过PHP code返回的文本。顺便说一句,我做的这种方式,因为我希望它能够工作在IE7和之后所有的浏览器。
I essentially want to be able to alert the text returned via the php code. Btw, I'm doing it this way because I want it to be able to work on IE7 and all browsers after.
我倒是AP preciate任何帮助吨。感谢您的时间!
I'd appreciate any help a ton. Thanks for the time!
推荐答案
嗯,我已经使用类似的东西:
Well, I've used something like that:
$("#upload_target")[0].contentWindow.document.body.innerHTML
...代替。但是,一般的方法是相似的:检查这个字符串,如果它是空的,就什么也不做(即立即返回)。否则,继续分析这种JSON。
... instead. But the general approach is similar: check this string, and if it's empty, just do nothing (i.e., return immediately). Otherwise proceed with analyzing this json.
这篇关于jQuery的文件上传隐藏的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!