Flex2.0实现文件上传功能(服务器为ASP.NET)
生活随笔
收集整理的這篇文章主要介紹了
Flex2.0实现文件上传功能(服务器为ASP.NET)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簡介:
??????? 新的Flex2.0類庫里提供了文件類,方便了上傳/下載文件。下面的程序demo演示了Flex2.0生成flash來訪問本地文件,在flash里上傳用戶選擇的文件到服務器,flash客戶端可以處理文件上傳進度等多個事件,服務器端是C#寫的文件接收模塊,把用戶上傳的文件保存在服務器上。
??????? Demo演示了ProgressEventType.PROGRESS, EventType.SELECT 2個事件的處理方法。
測試效果:
測試環境:
操作系統:windows2003 Server
Flex版本:Flex 2.0 Alpha 1
Flash版本: flash Player 8.5
WEB服務器:
????????? IIS 6.0
???????? .net FrameWork 1.1
客戶端代碼:FileUpload.mxml
<?xml?version="1.0"?encoding="utf-8"?>
<mx:Application?xmlns:mx="http://www.macromedia.com/2005/mxml"
????xmlns="*"?creationComplete="init();">
????<mx:Script>
????????<![CDATA[
????????????import?flash.net.FileReference;
????????????import?mx.controls.Alert;
????????????import?mx.events.AlertClickEvent;
????????????import?flash.events.*;
????????????var?file:FileReference;
????????????private?function?init(){
????????????????Security.allowDomain("*");
????????????????file?=?new?FileReference();
????????????????file.addEventListener(ProgressEventType.PROGRESS,?onProgress);
????????????????file.addEventListener(EventType.SELECT,?onSelect);
????????????}
????????????private?function?upload(){
????????????????file.browse();
????????????}
????????????
????????????private?function?onSelect(e:Event){
????????????????Alert.show("上傳?"?+?file.name?+?"?(共?"+Math.round(file.size)+"?字節)?",
???????????????????????????"確認上傳",
???????????????????????????Alert.YES|Alert.NO,
???????????????????????????null,
???????????????????????????proceedWithUpload);
????????????}
????????????
????????????private?function?onProgress(e:ProgressEvent){
????????????????lbProgress.text?=?"?已上傳?"?+?e.bytesLoaded?
????????????????????+?"?字節,共?"?+?e.bytesTotal?+?"?字節";
????????????}
????????????
????????????private?function?proceedWithUpload(e:AlertClickEvent){
????????????????if?(e.detail?==?Alert.YES){
????????????????????file.upload("http://localhost/JZService/WebForm1.aspx");????????????????????
????????????????}
????????????}
????????]]>
????</mx:Script>
????
????<mx:Canvas?width="100%"?height="100%">
????????<mx:VBox?width="100%"?horizontalAlign="center">
????????????<mx:Label?id="lbProgress"?text="上傳"/>
????????????<mx:Button?label="上傳文件"?click="upload();"/>????????????
????????</mx:VBox>
????</mx:Canvas>
</mx:Application>
服務端代碼:WebForm1.aspx
????????private?void?Page_Load(object?sender,?EventArgs?e)?{
????????????//?在此處放置用戶代碼以初始化頁面
????????????HttpFileCollection?uploadedFiles?=??Request.Files;
????????????string?Path?=?Server.MapPath("data");
????????????for(int?i?=?0?;?i?<?uploadedFiles.Count?;?i++)?{
????????????????HttpPostedFile?F?=?uploadedFiles[i];
????????????????if(uploadedFiles[i]?!=?null?&&?F.ContentLength?>?0)?{???
????????????????????string?newName?=?F.FileName.Substring(F.FileName.LastIndexOf("\\")?+?1);
????????????????????F.SaveAs(Path?+?"/"?+?newName);
????????????????}
????????????}
????????}
??????? 新的Flex2.0類庫里提供了文件類,方便了上傳/下載文件。下面的程序demo演示了Flex2.0生成flash來訪問本地文件,在flash里上傳用戶選擇的文件到服務器,flash客戶端可以處理文件上傳進度等多個事件,服務器端是C#寫的文件接收模塊,把用戶上傳的文件保存在服務器上。
??????? Demo演示了ProgressEventType.PROGRESS, EventType.SELECT 2個事件的處理方法。
測試效果:
測試環境:
操作系統:windows2003 Server
Flex版本:Flex 2.0 Alpha 1
Flash版本: flash Player 8.5
WEB服務器:
????????? IIS 6.0
???????? .net FrameWork 1.1
客戶端代碼:FileUpload.mxml
<?xml?version="1.0"?encoding="utf-8"?>
<mx:Application?xmlns:mx="http://www.macromedia.com/2005/mxml"
????xmlns="*"?creationComplete="init();">
????<mx:Script>
????????<![CDATA[
????????????import?flash.net.FileReference;
????????????import?mx.controls.Alert;
????????????import?mx.events.AlertClickEvent;
????????????import?flash.events.*;
????????????var?file:FileReference;
????????????private?function?init(){
????????????????Security.allowDomain("*");
????????????????file?=?new?FileReference();
????????????????file.addEventListener(ProgressEventType.PROGRESS,?onProgress);
????????????????file.addEventListener(EventType.SELECT,?onSelect);
????????????}
????????????private?function?upload(){
????????????????file.browse();
????????????}
????????????
????????????private?function?onSelect(e:Event){
????????????????Alert.show("上傳?"?+?file.name?+?"?(共?"+Math.round(file.size)+"?字節)?",
???????????????????????????"確認上傳",
???????????????????????????Alert.YES|Alert.NO,
???????????????????????????null,
???????????????????????????proceedWithUpload);
????????????}
????????????
????????????private?function?onProgress(e:ProgressEvent){
????????????????lbProgress.text?=?"?已上傳?"?+?e.bytesLoaded?
????????????????????+?"?字節,共?"?+?e.bytesTotal?+?"?字節";
????????????}
????????????
????????????private?function?proceedWithUpload(e:AlertClickEvent){
????????????????if?(e.detail?==?Alert.YES){
????????????????????file.upload("http://localhost/JZService/WebForm1.aspx");????????????????????
????????????????}
????????????}
????????]]>
????</mx:Script>
????
????<mx:Canvas?width="100%"?height="100%">
????????<mx:VBox?width="100%"?horizontalAlign="center">
????????????<mx:Label?id="lbProgress"?text="上傳"/>
????????????<mx:Button?label="上傳文件"?click="upload();"/>????????????
????????</mx:VBox>
????</mx:Canvas>
</mx:Application>
服務端代碼:WebForm1.aspx
????????private?void?Page_Load(object?sender,?EventArgs?e)?{
????????????//?在此處放置用戶代碼以初始化頁面
????????????HttpFileCollection?uploadedFiles?=??Request.Files;
????????????string?Path?=?Server.MapPath("data");
????????????for(int?i?=?0?;?i?<?uploadedFiles.Count?;?i++)?{
????????????????HttpPostedFile?F?=?uploadedFiles[i];
????????????????if(uploadedFiles[i]?!=?null?&&?F.ContentLength?>?0)?{???
????????????????????string?newName?=?F.FileName.Substring(F.FileName.LastIndexOf("\\")?+?1);
????????????????????F.SaveAs(Path?+?"/"?+?newName);
????????????????}
????????????}
????????}
轉載于:https://www.cnblogs.com/dannyr/archive/2006/01/19/320386.html
總結
以上是生活随笔為你收集整理的Flex2.0实现文件上传功能(服务器为ASP.NET)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DNF中为什么有的职业不是召唤也能召怪来
- 下一篇: 楚乔传左宝仓养的老鼠是什么品种 豚鼠是特