Home >Backend Development >PHP Tutorial >ajax--iframe simulates ajax file upload effect
js does not have the right to read local files, so cannot upload files, but
there are several methods
1 iframe disguises jquery-uploaded-file
2 swf plug-in (not to mention this, it is a separate software
3 html5
iframe simulates ajax file upload effect
1 Capture form submission action
2 Create iframe
3 Target modification points to iframe
4 Remove iframe
<h1></h1> <!-- enctype 属性可能的值: application/x-www-form-urlencoded multipart/form-data text/plain 规定在发送表单数据之前如何对其进行编码。 target 规定在何处打开 action URL。(这边是iframe里) onsubmit 点击运行js的方法通过才到`action 文件上传说没上传 enctype没写-->
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> function upfile(){ // 创建iframe 和属性name var ifname = 'up'+Math.random(); $('<iframe name="' + ifname +'">').appendTo($('body')); // form表单加入target属性,form点击在iframe打开 $('form:first').attr('target',ifname); } </script>php part If the error is 0 On behalf of the successful file upload, a message will appear in the h1 tag written before.
<?php if(empty($_FILES)){ exit('no file'); } $error = $_FILES['pic']['error'] == 0 ? '上传成功':'上传失败'; echo "<script> parent.document.getElementsByTagName('h1')[0].innerHTML = '$error'"; ?>
The above introduces the effect of ajax--iframe simulating ajax file upload, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.