按照我的代码:
.HTML:
<form id="test" action="javascript:test()" method="post">
<input name="image" type="file" />
<input type="submit" value="Submit" />
</form>
.JS:
function test(){
iframe = document.createElement("IFRAME");
iframe.name = "iframe_upload";
iframe.src="test.php";
document.body.appendChild(iframe);
document.getElementById("test").target = "iframe_upload";
}
.PHP:
<?php
if (isset($_FILES["image"])){
echo 'exist';
exit();
}
?>
错误在哪里?我确定问题出在javascript中,如果没有javascript动态创建iframe,它很容易起飞,但我必须使用javascript。
有几个
问题,但没什么大不了的,
首先,为了发布文件,您需要在表单中添加enctype="multipart/form-data"
表单的action
应该是您要发布到的文件(iframe源代码),并且JavaScript应该从onsubmit
运行
最后,您需要为iframe提供跨浏览器支持的id
我最终得到了这个
.JS:
function test(){
iframe = document.createElement("IFRAME");
iframe.name = "iframe_upload";
iframe.id = "iframe_upload"; //some browsers target by id not name
document.body.appendChild(iframe);
document.getElementById("test").target = "iframe_upload";
}
.HTML:
<form id="test" method="post" target="iframe_upload" enctype="multipart/form-data" onsubmit="javascript:test()" action="test.php">
<input name="image" type="file" />
<input type="submit" value="Submit" />
</form>
.PHP:
<?php
print_r($_FILES);
?>