日期:2014-05-16 浏览次数:20515 次



<div id="message" /></div>的后面添加一个文件上传按钮:
<div id="file-uploader"></div>
<div id="attachment_names"></div>
	<script id="attachment_names_tmpl" type="tmpl">
		<span id="attachment_${file.id}" class="attachment_name">
			<a href="##" name="${data.name}">${panda.shorten(file.name, 11)}</a> (${panda.formatSize(file.size)})
			<a href="##" class="remove" title="Remove"> </a>
		</span>
	</script><script src="js/lib/panda.js"></script>的后面增加对 panda.uploader.js 的引用。
<script src="js/lib/panda.uploader.js"></script>
	panda.uploader({
		// 指定文件上传按钮所在的div
		element: $("#file-uploader")[0],
		// 指定处理请求的方法
		params: { action: "hello.upload" },
		// 文件大小限制
		sizeLimit: 1000 * 1024 * 1024,
		// 上传结束的处理
		onComplete: function(file, data){
			// 上传结束,显示文件名称
			$("#attachment_names").append($("#attachment_names_tmpl").tmpl({
				file: file, data: data
			}));
			// 增加文件名称的点击事件
			var $attachment = $("#attachment_" + file.id);
			var $a = $attachment.find("a");
			// 点击文件名称,开始下载文件
			$a.eq(0).click(function() {
				panda.open({
					action: "hello.download",
					params: { name: $(this).attr("name") }
				});
			});
			// 点击 Remove 按钮,删除已上传的附件
			$a.eq(1).click(function(){ $attachment.remove(); });
		}
	});	// 文件上传
	upload: function(params, req) {
		// 检查文件大小
		var size = parseFloat(req.getHeader("Content-Length"));
		
		if (!size || size <= 0 ) {
			throw "Invalid Content-Length";
		} else if (size > 1000 * 1024 * 1024) {
			throw "File too large.";
		}
		// 生成文件名称
		var prefix = new Date().getTime() + "-"