gpt4 book ai didi

asp.net - 如何在 ASP.NET 中通过 Post 将信息传递到 iframe?

转载 作者:行者123 更新时间:2023-11-28 03:00:50 25 4
gpt4 key购买 nike

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

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

此数据将确定 iframe 中内容的布局,那么如何才能在发送帖子后更新 iframe 呢? (可能刷新?)

最佳答案

我写了一个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 在其中创建 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 中的概念。

关于asp.net - 如何在 ASP.NET 中通过 Post 将信息传递到 iframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com