<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>I@laoer.com &#187; Ajax</title>
	<atom:link href="http://i.laoer.com/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://i.laoer.com</link>
	<description>技术、生活、感悟 -- Laoer的博客</description>
	<lastBuildDate>Mon, 15 Feb 2010 08:36:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>用jQuery Form Plugin实现Ajax无刷新的文件上传</title>
		<link>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html</link>
		<comments>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html#comments</comments>
		<pubDate>Thu, 26 Feb 2009 08:37:13 +0000</pubDate>
		<dc:creator>Laoer</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://i.laoer.com/?p=120</guid>
		<description><![CDATA[在我以前的意识里，觉得用JavaScript或Ajax提交文件表单似乎是一件麻烦的事情，也没有太仔细的研究，后来看jQuery的时候，发现了jQuery Form Plugin这个插件，它方便的实现了Ajax方式的表单提交，例子里也包括了文件表单的提交，那我也来试一下吧，还是用Kohana（最近专注于这个，而且PHP开发要快多了，要是用Struts，要费好多功夫）。
要Kohana支持上传，先要配置一些upload的参数，把system\config\upload.php拷贝到application/config下，里面的参数做好设置，具体参考Kohana的文档吧，之后创建一个Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Myupload_Controller extends Top_Controller &#123;
&#160;
	public function index&#40;&#41; &#123;
		$view = new View &#40;&#34;upload&#34;&#41;;		
		$view-&#62;msg = &#34;Upload File&#34;;
&#160;
		$view-&#62;render &#40; TRUE &#41;;
	&#125;
&#160;
	public function upload&#40;&#41; &#123;		
		$filename = upload::save&#40;'myfile'&#41;;
		echo $filename;
&#160;
		exit &#40;&#41;;
	&#125;
&#160;
&#125;

Top_Controller为我自己创建的类，也继承自Kohana的Controller，之后创建upload.php的view文件

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
&#60;html&#62;
&#60;head&#62;
&#60;title&#62;&#60;?php echo $msg;?&#62;&#60;/title&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;&#60;?=url::base()?&#62;js/jquery-1.3.2.min.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;&#60;?=url::base()?&#62;js/jquery.form.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34;&#62; 
        // wait for the DOM to be loaded 
     [...]]]></description>
			<content:encoded><![CDATA[<p>在我以前的意识里，觉得用JavaScript或Ajax提交文件表单似乎是一件麻烦的事情，也没有太仔细的研究，后来看<a href="http://jquery.com/" target="_blank">jQuery</a>的时候，发现了<a href="http://malsup.com/jquery/form/" target="_blank">jQuery Form Plugin</a>这个插件，它方便的实现了Ajax方式的表单提交，例子里也包括了文件表单的提交，那我也来试一下吧，还是用Kohana（最近专注于这个，而且PHP开发要快多了，要是用Struts，要费好多功夫）。</p>
<p>要Kohana支持上传，先要配置一些upload的参数，把system\config\upload.php拷贝到application/config下，里面的参数做好设置，具体参考Kohana的文档吧，之后创建一个Controller</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Myupload_Controller <span style="color: #000000; font-weight: bold;">extends</span> Top_Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> View <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;upload&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
		<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Upload File&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">render</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">TRUE</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> upload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
		<span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> upload<span style="color: #339933;">::</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'myfile'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$filename</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">exit</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Top_Controller为我自己创建的类，也继承自Kohana的Controller，之后创建upload.php的view文件</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;?php echo $msg;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>js/jquery-1.3.2.min.js&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>js/jquery.form.js&quot;&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
        	var options = { 
        		target:        '#fileinfo'   // target element(s) to be updated with server response         
        	};
&nbsp;
        	$('#myForm').submit(function() { 
        		$(this).ajaxSubmit(options);
        		return false; 
        	});
&nbsp;
        }); 
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;&lt;?php echo $msg;?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myForm&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?=url::base()?&gt;</span></span>myupload/upload&quot; method=&quot;post&quot;
	enctype=&quot;multipart/form-data&quot;&gt;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;myfile&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit Comment&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;fileinfo&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

<p>在这里我们用jQuery Form Plugin来提交表单，之后将信显示在“fileinfo”这个div里，去试试效果吧。</p>
<p>在<a href="http://malsup.com/jquery/form/" target="_blank">http://malsup.com/jquery/form/</a>里有详细的API和例子，同时<a href="http://malsup.com/jquery/" target="_blank">http://malsup.com/jquery/</a>里也有其他的一些不错的jQuery的插件。</p>
]]></content:encoded>
			<wfw:commentRss>http://i.laoer.com/jquery-form-plugin-ajax-file-upload.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
