如何在ASP.NET中通过Post将信息传递给iframe?

如何在ASP.NET中通过Post将信息传递给iframe?,asp.net,javascript,jquery,iframe,postback,Asp.net,Javascript,Jquery,Iframe,Postback,我想通过post将信息传递给iframe。(可以是执行post的jquery或javascript,这并不重要) 信息无法通过querystring发送,因为我无权更改iframe引入的页面的方式 这些数据将决定iframe中内容的布局,那么我如何才能在发送帖子后更新iframe?(可能刷新?我写了一篇文章,介绍如何使用jQuery使用隐藏的iframe上传文件。代码如下: 以下是表单的HTML: <div id="uploadform"> <form id="theuploa

我想通过post将信息传递给iframe。(可以是执行post的jquery或javascript,这并不重要)

信息无法通过querystring发送,因为我无权更改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>

$(文档).ready(函数(){
$(“#表单提交”)。单击(函数(){
var userFile=$('form#userFile').val();
var max=$('form#max').val();
变量iframe=$('');
$('div#iframe')。追加(iframe);
$('#theuploadform').attr(“action”,“uploader.php”)
$(“#上传表单”).attr(“方法”,“发布”)
$('theuploadform').attr(“userfile”,userfile)
$(“#上传表单”).attr(“最大文件大小”,MAX)
$(“#上传表单”).attr(“enctype”,“多部分/表单数据”)
$(“#上传表单”).attr(“编码”,“多部分/表单数据”)
$(“#上传表单”).attr(“目标”、“后帧”)
$(“#上传表单”).submit();
//需要获取iframe的内容
$(“#postframe”).load(
函数(){
iframeContents=$(“iframe”)[0].contentDocument.body.innerHTML;
$(“div#textarea”).html(iframeContents);
} 
);
返回false;
});
});
我使用了一个类似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中的概念。

我写了一篇关于使用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>

$(文档).ready(函数(){
$(“#表单提交”)。单击(函数(){
var userFile=$('form#userFile').val();
var max=$('form#max').val();
变量iframe=$('');
$('div#iframe')。追加(iframe);
$('#theuploadform').attr(“action”,“uploader.php”)
$(“#上传表单”).attr(“方法”,“发布”)
$('theuploadform').attr(“userfile”,userfile)
$(“#上传表单”).attr(“最大文件大小”,MAX)
$(“#上传表单”).attr(“enctype”,“多部分/表单数据”)
$(“#上传表单”).attr(“编码”,“多部分/表单数据”)
$(“#上传表单”).attr(“目标”、“后帧”)
$(“#上传表单”).submit();
//需要获取iframe的内容
$(“#postframe”).load(
函数(){
iframeContents=$(“iframe”)[0].contentDocument.body.innerHTML;
$(“div#textarea”).html(iframeContents);
} 
);
返回false;
});
});
我使用了一个类似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
  }
}
函数postToPage(){
var iframe=document.getElementById('myIFrame');
if(iframe){
var newForm='document.forms[0].submit();';
iframe.document.write(newForm);//可能错误,请找到iframe的文档并对其进行写入
}
}

我手头没有代码,但我的团队完全是用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
  }
}
函数postToPage(){
var iframe=document.getElementById('myIFrame');
if(iframe){
var newForm='document.forms[0].submit();';
iframe.document.write(newForm);//可能错误,请找到iframe的文档并对其进行写入
}
}

Wow。所有这些代码都是为了这么简单?您可以使用表单的target属性,将其与IFRAME的name属性相关联,它将自然提交!哇!所有这些代码都是为了这么简单?您可以使用表单的target属性,将其与IFRAME的name属性相关联,它将自然提交!