<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Language" content="zh-TW">
<title>Asynchronous File Upload With YUI 非同步檔案上傳</title>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js"></script>
<script type="text/javascript">
function doSubmit(objForm){
// 第二個參數設成 true 允許檔案上傳
YAHOO.util.Connect.setForm(objForm, true);
var callback = {
upload: function(o) {
var result = YAHOO.lang.JSON.parse(o.responseText);
document.getElementById('log').innerHTML =
'name: ' + result.name + '<br>\n' +
'type: ' + result.type + '<br>\n' +
'size: ' + result.size + '<br>\n' +
'tmp_name: ' + result.tmp_name + '<br/>\n' +
'error: ' + result.error + '<br>\n';
}
};
// 同時使用 POST 與 GET (async) 判斷資料是由 AJAX 或表單直接送出
YAHOO.util.Connect.asyncRequest('POST', objForm.action + '?async=1', callback);
// 這裡傳回 false 並不能阻止 submit (原因未知)
return false;
}
</script>
</head>
<body>
<!-- 在 onSubmit 呼叫 doSubmit 後再傳回 false 避免以一般方式送出表單 -->
<form id="form_1" action="async_file_upload_action.php" enctype="multipart/form-data" method="post" onsubmit="doSubmit(this); return false;" >
<div>
<input type="file" id="input_file_1" name="input_file_1">
<input type="submit" id="input_submit_1" name="input_submit_1">
</div>
</form>
<pre id="log"></pre>
</body>
</html>
No comments:
Post a Comment
Comment Form Message