【问题标题】:Upload onload don't work on firefox iframe上传 onload 在 Firefox iframe 上不起作用
【发布时间】:2011-09-28 08:56:48
【问题描述】:

我在 Firefox 中遇到文件上传到 iframe 的问题。锚点应调用 函数 dosubf() 将表单发布到 iFrame 以上传 XML 文件,该文件稍后将使用 Ajax 解析并以另一个表单显示在同一页面上。

iframe 动态生成到 div 标签中。当它第一次加载时,工作正常,但是,在 Firefox 中,当将内容重新加载到 div 中时,会导致错误“var ret is null”!被萤火虫扩展捕获。我还注意到在 xmlhttp.open("POST", var, var.length) 调用上发生了另一个错误,该调用是为了将一个选择的 html 对象生成到表格单元格中。错误再次显示 some var 为空,但在这种情况下,删除程序的函数超出了范围!这两个函数在 IE9 中都可以正常工作。

对于第一个问题,我认为它可能与 onload 事件有关。第二次我真的不知道发生了什么。

下面是使用 Ajax 方法将 html 表单加载到 div 中的代码,用于 XML 文件的初始输入。

<form id='file_upload_form' method='post' 
enctype='multipart/form-data'   target='upload_targetf' 
action='include/php/util/handle/handlexml.php'>

<input name="file" id="file" type="file" />
<a href="#" class="botao" onclick="dosubf()">IMPORT</a>

<iframe  id='upload_target' name='upload_targetf' onload='lerxml()' ></iframe>
</form>

提交表单的按钮的onclick事件的javascript。

function dosubf(){
    if (document.getElementById('file').value==''){
        alert ("Por favor seleccione um arquivo");
        return
        }
    document.getElementById('file_upload_form').submit();
}

事件 onload 的函数在第一次加载到之前显示的 HTML 的 div 时触发良好,但是当我使用 Ajax 刷新 div 上的内容并尝试触发 onload 时失败。它第二次在火空 onload iFrame 上失败,当然在按钮操作上提交 onload。

function lerxml(){
    var ret = window.frames['upload_targetf'].document; 
//Here the code breaks with a ret is null error on firebug!
    var novostring='';
    var  d=ret.body.innerHTML;
    var mess='';
    if (d!=''){
        d=eval("("+d+")"); 
//This for getting the JSON response to evaluate the file
        if (d.tipo){
            mess+=(d.tipo);
        }
        if (d.tamanho){
            mess+=(d.tamanho);
        }
        var ok='';
        if (d.sucesso){
//The filepath of the uploaded file to use Ajax for parse it later
            novostring="arquivo/xml/"+d.sucesso;    
        }else{
            novostring="";
            alert (mess);
            return
        }
    }

评估文件上传到 iFrame 的 PHP

<?
$erro = $config = array();

$arquivo = isset($_FILES["file"]) ? $_FILES["file"] : FALSE;

$config["tamanho"] = 500000;

if($arquivo){     

if(!preg_match("/^text\/xml$/",$arquivo["type"]))    {      
$erro['tipo'] = "Não é um arquivo XML! Por favor seleccione outro arquivo"; 
}    else    {        

if($arquivo["size"] > $config["tamanho"])        {            
$erro['tamanho'] = "Arquivo em tamanho muito grande!";
} 

}

if(sizeof($erro))    {
 foreach($erro as $err)      {
  $err=htmlentities($err);
 }

echo json_encode($erro);
die;
}else    { 

$imagem_nome = md5(uniqid(time())) . ".xml";

$imagem_dir ['sucesso'] = "arquivo/xml/" . $imagem_nome; 
move_uploaded_file($arquivo["tmp_name"],"../../../../".$imagem_dir ['sucesso']);
echo json_encode(htmlentities($imagem_dir));
}

}
?>

嗯,我希望这已经足够清楚,可以得到你的帮助。

提前感谢您的任何 cmets。

【问题讨论】:

    标签: ajax firefox iframe upload


    【解决方案1】:

    我打赌你会遇到https://bugzilla.mozilla.org/show_bug.cgi?id=170799

    尝试将window.frames['upload_targetf'].document 替换为document.getElementById("upload_target").contentDocument,不会出现同样的问题。

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 2012-03-06
      • 2019-07-07
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2014-02-06
      • 2012-12-06
      相关资源
      最近更新 更多