PHP利用iframe上传文件并返回值到父框架

哈哈,淡水其实也是采用这样的做发法的。正好看到有童鞋博客上有这样写。就顺手贴过来了。

  1. if($result)    
  2. {   
  3. echo "上传成功!文件路径为:".$file2;   
  4. echo '<input name="img" type="hidden" value="'.$file2.'" id="img"/>';   
  5. echo'<script>parent.document.form1.img.value=document.getElementById("img").value;</script>';   
  6. }

文件上传成功后,把文件路径写入一个隐藏的域中(img),然后用DOM原理把隐藏域中的VALUE发送到父框架的表单form1的img文本框中:
ok,再来看看淡水的。
淡水编辑器用了tinyMCE,所以有一点点不一样。
我的form页:

  1. <script src="tiny_mce/tiny_mce.js" type="text/javascript"></script>
  2. <script type="text/javascript">
  3.     // O2k7 skin
  4.     tinyMCE.init({
  5.         // General options
  6.         mode : "exact",
  7.         elements : "content",
  8.         theme : "advanced",
  9.         skin : "o2k7",
  10.         language : "zh",
  11.         relative_urls : false,
  12. //....略过部分...
  13.  
  14.     });
  15.  
  16.     function InsertHtml(type,path){
  17.  
  18.         type=type.toLowerCase()
  19.  
  20.         switch(type){
  21.             case '.gif':
  22.             thecode = '<img src="'+path+'" alt=""/>';
  23.             break;
  24.             case '.jpg':
  25.             thecode = '<img src="'+path+'" alt=""/>';
  26.             break;
  27. //......略过部分......
  28.             default :
  29.             thecode = '<a href="'+path+'" target="_blank">Download</a>';
  30.             break;
  31.         }
  32.         tinyMCE.execCommand('mceInsertContent',true,thecode);
  33.     }
  34. </script>

iframe也就是在这个页面里了。
在我的iframe的提交给的php处理里:

  1. $pasteJS = "<script type=text/javascript>\n";
  2.             $pasteJS .= "parent.InsertHtml(\"" . $file_ext . "\",\"" . $upload_src . "\");\n";
  3.             $pasteJS .= "</script>\n";
  4.             echo $pasteJS;

于是上传的文件就跑到tinyMCE的编辑框里了。upload的文件路径也通过userdata会暂存起来,直到写入database。路径保存这块儿,也是在iframe的提交给的php处理文件里的,这里淡水就不多写了。

Leave a comment

2 Comments.

  1. 淡水河边

    忘了写那个童鞋的地址了,记录一下http://blog.wutongs.com/?action=show&id=13

  2. 不错不错。。我要试试,如果写了全部代码就好了。呵呵。。

Leave a Reply


[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word