Felx上传功能实现

最近在做flex需要做一个上传功能,但是用百度和谷歌搜索一下了相关资料,发现他们写的并不好,于是决定自己做一个。现在把实现的功能代码全部贴出来给大家共享。flex的上传必须要用.net或者java,php这些来配合最终的文件上传。我这里了用了.net。并且是相对路径,相对路径的话,需要把工程发布了才能上传,所以可以先建议用绝对路径,测试没问题了,改为相对路径。

首先在flex这边需要做,一个进度条,一个上传。

 

 private var fileRef:FileReference;
private const UPLOAD_URL:String="Default.aspx?ID=";

   /**
   * 文件上传
   *
   * @return null
   */
   private function upLoad(evt:Event):void
   {
    var imageType:FileFilter=new FileFilter("Images (*.jpg , *.jpeg , *.gif , *.png)", "*.jpg; *.jpeg; *.gif; *.png;")
    var ary:Array=new Array(imageType);
    addEventListenerForFileRef();
    fileRef.browse(ary);
   }

   /**
   * addEventListenerForFileRef
   *
   * @return null
   */
   private function addEventListenerForFileRef():void
   {
    fileRef.addEventListener(Event.OPEN, openHandler);
    fileRef.addEventListener(Event.SELECT, selectHandler);
   }

   /**
   * 选择上传的文件
   *
   * @return null
   */
   private function selectHandler(evt:Event):void
   {
    fileRef.removeEventListener(Event.SELECT, selectHandler);

    var request:URLRequest=new URLRequest();

    request.url=UPLOAD_URL + m_strStuID;
    if (fileRef.size >= 1024 * 512)
    {
     Alert.show("上传文件不能大于512KB", "警告", Alert.YES)
     return ;
    }
    fileRef.upload(request);
   }

   /**
   * 上传进度
   *
   * @return null
   */
   private function openHandler(evt:Event):void
   {
    fileRef.removeEventListener(Event.OPEN, openHandler);

    var processDialog:ProcessDialog=ProcessDialog(PopUpManager.createPopUp(this, ProcessDialog, true));
    processDialog.addEventListener(ProcessDialogEvent.GC_ON_COMPLETE, completeHandler);
    processDialog.FileReference=fileRef;

    processDialog.title="正在上传,请稍候....";

    PopUpManager.centerPopUp(processDialog);
   }

然后是进度条代码

package soesc0001.codebehind
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileReference;

import mx.containers.TitleWindow;
import mx.controls.Button;
import mx.controls.ProgressBar;
import mx.managers.PopUpManager;

import soesc0001.event.ProcessDialogEvent;

[Event(name="Complete", type="soesc0001.event.ProcessDialogEvent")]

public class ProcessDialogClass extends TitleWindow
{
   public var pgbProgress:ProgressBar;

   public var btnCancel:Button;

   private var fileRef:FileReference;

   private var operationType:String;

   public function ProcessDialogClass()
   {
    super();
   }

   public function set FileReference(value:FileReference):void
   {
    fileRef=value;

    fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    fileRef.addEventListener(Event.COMPLETE, completeHandler);
   }

   public function set OperationType(value:String):void
   {
    operationType=value;
   }

   protected function btnCancel_onClick(event:MouseEvent):void
   {
    if (btnCancel.label == "Cancel")
    {
     fileRef.cancel();
    }

    closeWindow();
   }

   private function closeWindow():void
   {
    PopUpManager.removePopUp(this);
   }

   private function progressHandler(event:ProgressEvent):void
   {
    pgbProgress.label="UPLOADING %3%%";

    pgbProgress.setProgress(event.bytesLoaded, event.bytesTotal);
   }

   private function completeHandler(event:Event):void
   {
    pgbProgress.label="上传完毕。";

    btnCancel.label="Close";

    var evt:ProcessDialogEvent=new ProcessDialogEvent(ProcessDialogEvent.GC_ON_COMPLETE);
    dispatchEvent(evt);
   }
}
}

进度条的自定义事件
 public class ProcessDialogEvent extends Event
 {
  
  public static const GC_ON_COMPLETE:String="Complete";
  
  public function ProcessDialogEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
  {
   super(type, bubbles, cancelable);
  }
  
 }
最后是.net最终实现文件上传
public partial class _Default : System.Web.UI.Page
{
    private const string uploadFiles = "upLoadFiles";

    protected void Page_Load(object sender, EventArgs e)
    {
        HttpFileCollection files = Request.Files;

        if (files.Count == 0)
        {
            Response.Write("请勿直接访问本文件");
            Response.End();
        }

        string strStuID = Request.QueryString["ID"].Trim();
        string path = Server.MapPath(uploadFiles) + "/" + strStuID;

        HttpPostedFile flie = files[0];

        try
        {
            if (Directory.Exists(path) == false)
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }
        }
        catch (Exception)
        {
            return;
        }

        if (flie != null && flie.ContentLength > 0)
        {
            string savePath = path + "/" + Request.Form["fileName"];
            flie.SaveAs(savePath);
        }
    }

}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值