티스토리 뷰

AJAX file upload

Ever wanted to upload files using AJAX like in GMAIL, without reloading the page? Now you can. Cross browser method to upload files using AJAX in only 1Kb of code.

You need to create a form with file fields you wish to upload and define “onsubmit” event. Look at the example below how to do that.

Tested in IE5.5+, FF1.0+, OP 8.0+

DEMO

Source code for index.html

 
<script src="webtoolkit.aim.js" type="text/javascript"><!--mce:0--></script>
<script type="text/javascript"><!--mce:1--></script>
 
<form action="index.php" method="post">
 
<div><label>Name:</label>
<input name="form[name]" type="text" /></div>
 
 
<div><label>File:</label>
<input name="form[file]" type="file" /></div>
 
 
<div>
<input type="submit" value="SUBMIT" /></div>
 
</form>
<hr />
 
<div># of submited forms: <span id="nr">0</span></div>
 
 
<div>last submit response (generated by form action - index.php file): 
</div>


Source code for webtoolkit.aim.js

/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/
 
AIM = {
 
	frame : function(c) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '';
		document.body.appendChild(d);
 
		var i = document.getElementById(n);
		if (c &amp;&amp; typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
 
		return n;
	},
 
	form : function(f, name) {
		f.setAttribute('target', name);
	},
 
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c &amp;&amp; typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
 
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}
 
}

Source code for index.php

print_r($_REQUEST['form']); 

참조 : http://www.webtoolkit.info/ajax-file-upload.html

'WEB 전체 > JS' 카테고리의 다른 글

JQuery 달력 (datepicker 이용)  (0) 2011.11.02
JQuery - ajax 한글파라미터 전송 해결법  (0) 2011.10.28
Javascript trim function  (0) 2011.10.11
JQuery 이용한 이미지 rolling  (2) 2011.10.06
RADIO 버튼 선택 체크  (0) 2011.10.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함