我想通过帖子将信息传递给iframe。 (可能是执行帖子的jquery或javascript,它并不重要)。

无法通过查询字符串发送信息,因为我无权更改iframe引入页面的方式。

此数据将决定iframe中内容的布局,以便我如何制作以便在发送帖子后更新iframe? (可能刷新?)

有帮助吗?

解决方案

我写了一篇博文关于使用jQuery执行此操作以使用隐藏的iframe上传文件。这是代码:

以下是表单的HTML:

<div id="uploadform">
<form id="theuploadform">
<input type="hidden" id="max" name="MAX_FILE_SIZE" value="5000000" >
<input id="userfile" name="userfile" size="50" type="file">
<input id="formsubmit" type="submit" value="Send File" >
</form>

允许jQuery创建iframe的DIV你可以用一点CSS隐藏它:

<div id="iframe" style="width:0px height:0px visibility:none">
</div>

显示回调结果的DIV:

<div id="textarea">
</div>

jQuery代码:

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    $("#formsubmit").click(function() {
        var userFile = $('form#userfile').val();
        var max = $('form#max').val();
        var iframe = $( '<iframe name="postframe" id="postframe" class="hidden" src="about:none" />' );
        $('div#iframe').append( iframe );

        $('#theuploadform').attr( "action", "uploader.php" )
        $('#theuploadform').attr( "method", "post" )
        $('#theuploadform').attr( "userfile", userFile )
        $('#theuploadform').attr( "MAX_FILE_SIZE", max )
        $('#theuploadform').attr( "enctype", "multipart/form-data" )
        $('#theuploadform').attr( "encoding", "multipart/form-data" )
        $('#theuploadform').attr( "target", "postframe" )
        $('#theuploadform').submit();
        //need to get contents of the iframe
        $("#postframe").load(
            function(){
                iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
                $("div#textarea").html(iframeContents); 
            } 
        );
        return false;
    });
});

</script>

我使用像这个uploader.php这样的php应用程序来处理文件:

<?php

$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$maxfilesize = $_POST[MAX_FILE_SIZE];

if ($maxfilesize > 5000000) {
//Halt!
   echo "Upload error:  File may be to large.<br/>";
   exit();
}else{
    // Let it go
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   print('File is valid, and was successfully uploaded. ');
} else {
   echo "Upload error:  File may be to large.<br/>";
}

chmod($uploadfile, 0744);
?>

除此之外还有你需要的东西,但它在jQuery中说明了这个概念。

其他提示

我没有方便的代码,但我的团队完全用Javascript完成了这个。我记得它是这样的:

function postToPage() {
  var iframe = document.getElementById('myIFrame');

  if (iframe) {
    var newForm = '<html><head></head><body><form...> <input type="hidden" name="..." value="..." /> </form><script type=\"text/javascript\">document.forms[0].submit();</scrip' + 't></body></html>';

    iframe.document.write(newForm);  //maybe wrong, find the iframe's document and write to it
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top