2009-06-16 51 views
1

我想通过信息将信息传递给iframe。 (可能是执行该帖子的jQuery或JavaScript,它并不重要)。如何通过Post in ASP.NET将信息传递给iframe?

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

这些数据将决定iframe中内容的布局,所以我怎样才能让它在发送帖子后更新iframe? (可能刷新?)

回答

1

我写了一个blog post关于如何使用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的创建,你可以用一点CSS隐藏的iframe的DIV:

<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> 

我使用的PHP应用程序这样uploader.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的概念。

+1

哇。所有这些代码如此简单?您可以使用FORM的target属性,将其与IFRAME的name属性关联起来,并自然提交! – 2009-06-24 19:38:40

0

我没有代码方便,但我的团队纯粹在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 
    } 
}