简体   繁体   English

上传“ AJAX”上传技巧后,iframe没有更新

[英]iframe doesn't get updated after the upload 'AJAX' upload trick

I am using the iframe technique to upload a file contained in a form. 我正在使用iframe技术上传表单中包含的文件。

It's working perfectly, I even send myself an email with the file. 它运行良好,我什至给自己发送了一封包含该文件的电子邮件。 The only problem is that I want to get infos from the iframe, such as file size. 唯一的问题是,我想从iframe获取信息,例如文件大小。 However, I can't update the iframe's content from the php script. 但是,我无法从php脚本更新iframe的内容。 I tried putting 'echos' on the line before it sends the email (so it actually gets there), but as soon as the iframe's jQuery's "load" function fires, there is nothing in the iframe. 我尝试在发送电子邮件之前将“ echos”放在行上(这样它实际上就到达了),但是一旦iframe的jQuery的“ load”函数启动,iframe中就什么也没有了。 Even tried "Inspect element" and "view source", all empty. 甚至尝试过“检查元素”和“查看源代码”,都为空。

Am I missing something obvious? 我是否缺少明显的东西? I can't find anyone with the same problem on SO or google. 我在SO或Google上找不到任何有相同问题的人。

FORM: 形成:

<form id="submitResumeForm" method="post" enctype="multipart/form-data" action="career/sendResume" target="submitTarget">
<!-- input fields, etc -->
<input type="button" onClick="submitForm()" value="<?=lang('submit')?>" />
</form>
<iframe id="submitTarget" name="submitTarget" src="" style="position:absolute;top:-1000px;left:-1000px;width:0;height:0;border:0px solid #fff;"></iframe>

JAVASCRIPT: JAVASCRIPT:

function submitForm()
{
    $('#submitResumeForm').attr('target', 'submitTarget');
    // ... verifications ...
    $('#submitTarget').load(function()
    {
        console.log( $(this).html() ); // Content is: "content before"
        console.log( $(this).contents() ); //Garbage
    });
    // Line Below puts content in the iframe, and that content is still there after
    // the php is executed, so iframe is NEVER updated
    $('#submitTarget').html('content before');

    $("#submitResumeForm").submit();
}

PHP CONTROLLER: PHP控制器:

function sendResume()
{
    echo 'ASDFASDFASDFASDF';
    $this->load->library('email');
    $this->email->clear();      
    $this->email->to($this->input->post('email'));
    // ... other fields, attach file (working perfectly)
    $this->email->send();
}

Here's the solution: The line 解决方法如下:

console.log( $('#submitTarget').html() ); // Display iframe's content

Is NOT updated in the DOM for some reason I don't know, you have to use this if this ever happens to you: 由于某些原因,我没有在DOM中进行更新,如果您遇到这种情况,则必须使用它:

console.log( $('#submitTarget').contents().find('body').html() );

i think you are doing it wrong... 我认为您做错了...
what you should do is something like 你应该做的像

<form ...>
   <input type="file" .../>
   <input type="submit" .../>
</form>
<iframe id="myIframe"></iframe>

$('form').submit(function(){
   $(form).attr('target'. 'myIframe');
});

I don't know if this will work, but i remember a long time ago i tried it somehow as you are doing it and i had to change it to the way i do today. 我不知道这是否行得通,但我记得很久以前,我在尝试时以某种方式尝试了一下,因此不得不将其更改为今天的方式。
Just for kicks, can you try to change the input-button to an input-submit removing the onclick and putting the code inside your submitForm() function into the submit event of the form as i do above (removing your last line invoking submit() and $('#submitTarget').load(function() and $('#submitTarget').html('content before');)? 只是为了踢一下,您能像我上面所做的那样尝试将输入按钮更改为输入提交,从而删除onclick并将代码放入表单的Submit事件中,并将onClickForm()函数中的代码放入表单的Submit事件中(删除最后一行调用Submit( )和$('#submitTarget')。load(function()和$('#submitTarget')。html('content before');)?

you just need to change the target, really 你只需要改变目标,真的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM