繁体   English   中英

使用IFRAME上传文件,该文件不会到达服务器

[英]Upload file with IFRAME, the file don't get to the server

按照我的代码:

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"添加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);
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM