Home >Backend Development >PHP Tutorial >ajax--iframe simulates ajax file upload effect

ajax--iframe simulates ajax file upload effect

WBOY
WBOYOriginal
2016-07-29 09:05:15720browse

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没写-->
	

js part (using jquery) Create iframe

<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 = &#39;up&#39;+Math.random();
		$(&#39;<iframe name="&#39; + ifname +&#39;">&#39;).appendTo($(&#39;body&#39;));
		// form表单加入target属性,form点击在iframe打开
		$(&#39;form:first&#39;).attr(&#39;target&#39;,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(&#39;no file&#39;);
}

$error = $_FILES[&#39;pic&#39;][&#39;error&#39;] == 0 ? &#39;上传成功&#39;:&#39;上传失败&#39;;
echo "<script> parent.document.getElementsByTagName('h1')[0].innerHTML = '$error'";

?>

ajax--iframe simulates ajax file upload effect

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn