<p>文件的上传与下载主要依靠FileReference和FileReferenceList这两个类来实现,包结构为falsh.net.FileRefenence和flash.net.FileReferenceList.</p> <h2>文件的下载</h2> <h4>FileReference对象的downLoad()方法</h4> <blockquote> <p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:3ce9b2ff-18ca-44b8-99e0-0062434c4af2" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: php; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 878px; height: 18px;" style=" width: 878px; height: 18px;overflow: auto;"> downLoad(request:URLRequest,defaultFileName:String)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> </p>
<ul> <li><font size="3"><font style="font-weight: normal">request的url属性应包含为要下载到本地的文件的URL;</font></font></li> </ul> </blockquote>
<ul> <li><font size="3"><font style="font-weight: normal">defaultFileName为文件下载到本地后的文件名。最好手动指定。文件名不能包含以下字符:/\:*'”<>|%</font></font></li>
<li><font style="font-weight: normal" size="3">此方法打开一个操作系统对话框,用户选择了位置并确认现在后开始下载。</font></li>
<li>为了在downLoad()方法执行后跟踪下载操作的状态,我们要实现对open、progress和complete事件进行侦听。</li> </ul>
<p>我们还可以侦听fileReference对象的cancel,select事件进行侦听,以确定用户是点击了确定还是取消按钮。</p>
<blockquote> <p>监听下载进度</p> </blockquote>
<blockquote> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:10ecf010-cfd6-499e-8beb-44b494e5f49f" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: php; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 878px; height: 158px;" style=" width: 878px; height: 158px;overflow: auto;">var fileRef:FileReference=new FileReference(); fileRef.addEventListener(ProgressEvent.PROGRESS,onProgress); fileRef.downLoad(url,fileName); …… private function onProgress(event:ProgressEvent):void{
var loaded:uint=event.bytesLoaded;
var total:uint=event.bytesTotal;
……
}</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>
</blockquote>
<p> </p>
<p> </p>
<h2>文件的上传</h2>
<h4>FileReference或FileReferenceList的browse()与upLoad()方法。</h4>
<p> </p>
<h4>FileReference或FileReferenceList的browse()方法</h4>
<blockquote> <p> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:a711777c-4d55-42da-adc5-4b08afe49ef5" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: php; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 878px; height: 22px;" style=" width: 878px; height: 22px;overflow: auto;">browse(typeFilter:Array):Boolean</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> </p>
<p> </p>
<ul> <li>typeFilter为文件滤镜,即指定哪几种类型的文件可以在打开的浏览对话框中显示。</li> </ul> </blockquote>
<ul> <li>此方法打开一个对话框用语浏览本地磁盘文件。FileReference只能单选,FileReferenceList能实现多选。</li>
<li>当调用此方法并成功选择了文件时,会填充FileReference对象的属性以在upLoad()方法中使用。</li>
<li>此方法可能会抛出异常,所以最好在 try-catch语句中进行捕获处理。</li> </ul>
<blockquote> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:fc4d4a2e-b446-4670-8ee1-585abdb7404a" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: php; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 878px; height: 40px;" style=" width: 878px; height: 40px;overflow: auto;"> var fileRef:FileReference=new FileReference();//实例化对象 fileRef.browse([new FileFilter("psd文件","*.psd")]);//弹出浏览窗口,只能显示psd类型的文件</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> </blockquote>
<p>在设置过滤器后,可能会发生ArguementError异常,可能是由于FileFilter对象的格式错误造成的。</p>
<h4>FileReference的upLoad()方法</h4>
<blockquote> <div id="scid:9D7513F9-C04C-4721-824A-2B34F0212519:5b49a00b-6693-4fdb-bdd8-636e1572f16b" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px"><pre class="brush: php; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 878px; height: 21px;" style=" width: 878px; height: 21px;overflow: auto;">upload(request:URLRequest,upLoadDataFileName:String=”fileData”,testUpload=false)</pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> </blockquote>
<p>request 此URLRequest对象的url属性应包含特定服务器脚本的URL,该服务器脚本呗配置为通过HTTP POST调用处理上传。</p>