asp、.net专栏

lvlingwy

flash+asp.net上传文件,显示进度条

     最近由于项目的需求,对flash做了一番研究,并结合Asp.net实现RIA开发。以下是用flash作为客户端(文件上传),通过asp.net作为服务器端(实现文件接收)的一个实例:
      一、客户端基本实现流程:
      1、flash设计界面如下图(1)所示:
         
                                                图(1)
      2、flash运行界面如下图(2)所示:
      
                                                图(2)
      3、在flash中单击“浏览”按钮,弹出以下对话框(图(3)),然后选将要上传的文件;
      
                                                图(3)
      4、选择好要上传的文件然后点击打开;(比如我选择guizu.wmv)图(4):
      

                                                图(4)
      5、然后点击上传,接着执行上传过程(图(5)):
      
                                                图(5)
      二、flash核心代码如下:
      ////import flash.net.FileReference;
      ////import flash.net.FileReferenceList;
      ////
      ////var fileRefList:FileReferenceList = new FileReferenceList();
      ////var totalBytes:Number = 0;
      ////var uploadedBytes:Number = 0;
      ////var uploadedBytes2:Array;
      ////var filesCompleted:Number = 0;
      ////var totalFiles:Number = 0;
      ////var fileRefListener:Object = new Object();
      ////
      ////var allTypes:Array = new Array();
      ////var videoTypes:Object = new Object();
      ////videoTypes.description = "视频文件(*.wmv; *.asf; *.avi; *.mpg; *.3pg;*.mov)";
      ////videoTypes.extension = "*.wmv; *.asf; *.avi; *.mpg; *.3pg;*.mov";
      ////allTypes.push(videoTypes);
      ////
      ////uploadBtn.enabled = false;
      ////_root.progressBar.visible = false;
      ////
      ////
      ////function rePaintProBar (pb, xCor, yCor) {
      //// _root.destroyObject(pb);
      //// _root.createObject("ProgressBar",pb,0);
      ////    _root.progressBar.move(xCor,yCor);
      //// _root.progressBar.label = "正在上传 %3%% ";
      //// _root.progressBar.labelPlacement = "right";
      //// _root.progressBar._width = 359;
      ////}
      ////
      ////fileRefListener.onSelect = function (fileRefList:FileReferenceList):Void {
      //// uploadBtn.enabled = true;
      //// rePaintProBar("progressBar", 109.0, 41.0);
      //// var list:Array = fileRefList.fileList;
      ////    var fileRef:FileReference;
      //// totalBytes = 0;
      ////    for(var i:Number = 0; i < list.length; i++) {
      ////        fileRef = list[i];
      ////  totalBytes += fileRef.size;
      //// txtFilePath.text = fileRef.name;
      ////    }
      ////}
      ////
      ////fileRefListener.onCancel = function (fileRef:FileReference):Void {
      //// uploadBtn.enabled = false;
      ////}
      ////
      ////fileRefListener.onOpen = function (fileRef:FileReference):Void { 
      //// txt_FilePath.label = fileRef.name;
      ////}
      ////
      ////fileRefListener.onProgress = function (fileRef:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
      //// progressBar.mode = "manual";
      //// var temp:Number = bytesLoaded - uploadedBytes2[fileRef.name]
      //// uploadedBytes2[fileRef.name] = bytesLoaded;
      //// uploadedBytes += temp;
      //// progressBar.setProgress(uploadedBytes, totalBytes);
      //// txtUploaded.text = GetSizeType(uploadedBytes);
      //// txtTotal.text = GetSizeType(totalBytes);
      ////
      ////}
      ////
      ////fileRefListener.onComplete = function (fileRef:FileReference):Void {
      //// filesCompleted++;
      //// if(filesCompleted == totalFiles)
      ////  FinishedUpload();
      ////}
      ////
      ////fileRefListener.onHTTPError = function(fileRef:FileReference):Void {
      //// trace("onHTTPError:"+ fileRef.name);
      ////}
      ////
      ////fileRefListener.onIOError = function(fileRef:FileReference):Void {
      ////    trace("onIOError: " + fileRef.name);
      ////
      ////}
      ////
      ////fileRefListener.onSecurityError = function(fileRef:FileReference, errorString:String):Void {
      ////    trace("onSecurityError: " + fileRef.name + " errorString: " + errorString);
      ////}
      ////
      ////fileRefList.addListener(fileRefListener);
      ////
      ////browseButn.clickHandler = function () {
      //// fileRefList.browse(allTypes);
      ////}
      ////
      ////uploadBtn.clickHandler = function () {
      //// var list:Array = fileRefList.fileList;
      //// var fileRef:FileReference;
      //// if(uploadBtn.label == "上传")
      //// {   
      ////  browseBtn.enabled = false;
      ////  uploadBtn.label = "取消";
      ////  totalFiles = list.length;
      ////  filesCompleted = 0;
      ////  uploadedBytes = 0;
      ////  uploadedBytes2 = [];
      ////  txtUploadNum.text = "已上传:";
      ////  txtFileTotal.text = "文件大小:";
      ////  for(var i:Number = 0; i < list.length; i++) {
      ////   fileRef = list[i];
      ////   fileRef.addListener(fileRefListener);
      ////   uploadedBytes2[fileRef.name] = 0;
      ////   if(uploadPage != undefined)
      ////    fileRef.upload(uploadPage);
      ////  }
      //// }
      //// else
      //// {  
      ////  for(var i:Number = 0; i < list.length; i++) {
      ////   fileRef = list[i];
      ////   fileRef.cancel();
      ////  }
      ////  uploadBtn.label = "上传";
      ////  browseBtn.enabled = true;
      //// }
      ////}
      ////
      ////function FinishedUpload()
      ////{
      //// uploadBtn.enabled = false;
      //// uploadBtn.label = "上传";
      //// browseBtn.enabled = true;
      //// if(completeFunction != undefined)
      //// {
      ////  getURL('javascript:' + completeFunction);
      //// }
      ////}
      ////
      ////function GetSizeType(size:Number)
      ////{
      //// if(size < 1024)
      ////  return int(size*100)/100 + " bytes";
      //// if(size < 1048576)
      ////  return int((size / 1024)*100)/100 + "KB";
      //// if(size < 1073741824)
      ////    return int((size / 1048576)*100)/100 + "MB";
      ////  return int((size / 1073741824)*100)/100 + "GB";
      ////}

      三、服务器端Asp.net实现核心代码如下:
       ////using System;
       ////using System.Data;
       ////using System.Configuration;
       ////using System.Collections;
       ////using System.Web;
       ////using System.Web.Security;
       ////using System.Web.UI;
       ////using System.Web.UI.WebControls;
       ////using System.Web.UI.WebControls.WebParts;
       ////using System.Web.UI.HtmlControls;
       ////
       ////public partial class Upload : System.Web.UI.Page
       ////{
       ////    protected void Page_Load(object sender, EventArgs e)
       ////    {
       ////        string saveToFolder = "Upload";
       ////        HttpFileCollection uploadedFiles = Request.Files;
       ////        string Path = Server.MapPath(saveToFolder);
       ////        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);
       ////            }
       ////        }
       ////    }

       本实例目前还只实现了文件上传功能,还未对文件种种业务需求的判断与处理以及文件上传后的数据处理等等,待今后在项目中具体扩充......

 
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭