`
lichen0921
  • 浏览: 81189 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

flex多文件带进度条上传

    博客分类:
  • flex
阅读更多
 
 
这个我做的是弹出的TitleWindow
主页代码简单,就是一个按钮
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"    
            xmlns:s="library://ns.adobe.com/flex/spark"    
            xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768">
  <s:layout>
    <s:BasicLayout/>
  </s:layout>
  <fx:Script>
    <![CDATA[
      import com.lichen.component.upload;
      import mx.managers.PopUpManager;
      protected function button1_clickHandler(event:MouseEvent):void
      {
        var win:upload=upload(PopUpManager.createPopUp(this,upload,true));
        PopUpManager.centerPopUp(win);
      }
    ]]>
  </fx:Script>
  <s:Button x="36" y="47" label="按钮" click="button1_clickHandler(event)"/>
</s:Application>
 
弹出的TitleWindow是核心
<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo"    layout="absolute" title="上传文件" color="#7dbded"
        width="609" height="460" baseColor="#E0ECFF" borderColor="#E0ECFF" backgroundColor="haloBlue" backgroundAlpha="0.1" contentBackgroundAlpha="0.1"
        borderVisible="true" dropShadowVisible="true"    
        showCloseButton="true" close="PopUpManager.removePopUp(this)"
         fontFamily="Arial" fontSize="14" fontWeight="bold" verticalAlign="top" initialize="init();">
  <fx:Script>
    <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.controls.Alert;
      import mx.controls.ProgressBar;
      import mx.managers.PopUpManager;
        
      private var fileList:FileReferenceList = new FileReferenceList();  //多选文件的list
      private var urlRequest:URLRequest = new URLRequest("http://192.168.1.205:9218/productshowshow/upload");    //处理文件上传的servlet
      [Bindable]
      public var selectedFiles: ArrayCollection = new ArrayCollection([]);    //多选的文件分离成文件数组
      private var fileListSize:uint=1;
      /**
        * 初始化
        */
      public function init():void{
        fileList.addEventListener(Event.SELECT,fileSelectHandler);
      }
      /**
        *监听文件选择的处理函数
        */
      private function fileSelectHandler(event: Event): void
      {
        if(fileList.fileList.length>10){
          Alert.show("一次最多只能上传10个文件");
        }else{
          for(var i:int=0;i<fileList.fileList.length;i++){
            var f:FileReference = FileReference(fileList.fileList[i]);    
            var obj:Object= new Object();
            obj.fileName = f.name;
            obj.fileSize = f.size/1024;
            obj.fileType = f.type;
            obj.fileRefrence = f;
            fileListSize+=f.size;
            selectedFiles.addItem(obj);
          }
          if( fileListSize/(1024*1024)>100 ){
            selectedFiles.removeAll();
            Alert.show("文件总大小应小于100M");
          }
        }
//        for each (var file: FileReference in fileList.fileList)
//        {
//          selectedFiles.addItem(file);
//        }
      }
      /**
        * 点击"浏览"按钮-->选择文件
        */
      protected function selectFile():void
      {
        //浏览文件,因为是FileReferenceList所以可以多选. 并用FileFilter过滤文件类型.
        fileList.browse([new FileFilter("图片文件 (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png"),new FileFilter("所有文件 (*.*)","*.*")]);
      }
      /**
        * 逐个上传文件
        */
      private function uploadHandler(event:MouseEvent):void{
        var file:FileReference;
        for(var i:int = 0 ;i<selectedFiles.length;i++){
          file = FileReference(selectedFiles[i].fileRefrence);
          file.upload(urlRequest);
        }
      }
      /**
        * 上传一个文件,监听文件上传完成事件,递归调用.
        */
      private function doSingleUploadFile():void{
        if(selectedFiles.length>10){
          selectedFiles.removeAll();
          Alert.show("一次最多只能上传10个文件");
        }else if( fileListSize/(1024*1024)>100 ){
          selectedFiles.removeAll();
          Alert.show("文件总大小应小于100M");
        }else{
          if (fileList.fileList.length > 0){
            var f: FileReference = fileList.fileList.shift() as FileReference;
            f.addEventListener(Event.COMPLETE, doSingleUploadFileComplete);
            f.upload(urlRequest);
          }
        }
      }
      /**
        * 一个文件上传完成事件的处理函数,递归执行上传下一个文件.
        */
      private function doSingleUploadFileComplete(event: Event):void{
        var file: FileReference = event.target as FileReference;
        file.removeEventListener(Event.COMPLETE, doSingleUploadFileComplete);
        doSingleUploadFile();
      }
      public function removeFile(f: FileReference): void
      {
        var index: int = selectedFiles.getItemIndex(f);
        if (index != -1)
          selectedFiles.removeItemAt(index);
      }
        
    ]]>
  </fx:Script>
  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>
  <mx:LinkButton x="10" y="23" textRollOverColor="purple" textSelectedColor="haloBlue"    label="选择本地文件" cornerRadius="7" fontFamily="中易宋体" fontSize="14" fontWeight="bold" fontStyle="normal" textDecoration="none" textAlign="center"
             click="selectFile()"/>
  <mx:DataGrid x="12" y="50" id="dg" width="587" height="291" color="#7dbded" contentBackgroundColor="#E0ECFF" baseColor="#E0ECFF"
        dataProvider="{selectedFiles}">
    <mx:columns>
      <mx:DataGridColumn headerText="文件名" dataField="fileName" width="150"/>
      <mx:DataGridColumn headerText="大小(kb)" dataField="fileSize" width="100"/>
      <mx:DataGridColumn headerText="类型" dataField="fileType" width="70"/>
      <mx:DataGridColumn headerText="上传状态" dataField=""    width="230">
        <mx:itemRenderer>
          <fx:Component>
            <mx:HBox width="130" paddingLeft="2" horizontalGap="2">
              <fx:Script>
                <![CDATA[
                  override public function set data(value:Object):void{
                    super.data = value;
                    data.fileRefrence.addEventListener(ProgressEvent.PROGRESS,progressHandler);
                    data.fileRefrence.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fini);
                  }
                    
                  private function progressHandler(event:ProgressEvent):void{
//                    progress.setProgress(event.bytesLoaded,data.fileRefrence.size);
//                    Alert.show("hahah");
//                    progress.setProgress(event.bytesLoaded, event.bytesTotal);
                    var procent:uint=event.bytesLoaded/event.bytesTotal*100;
//                    progress.label=procent+"###";
//                    if(procent==100){
////                      progress.enabled=false;
//                      progress.label="完成啦";
//                    }
//                    data.fileRefrence.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
                  }    
                    
                  public function fini(event: DataEvent):void{
                    progress.visible=true;
                    progress.label="完成";
                    data.fileRefrence.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,fini);
                  }
                ]]>
              </fx:Script>
              <mx:ProgressBar id="progress" width="80%"    
                      minimum="0" maximum="100" source="{data.fileRefrence}"
                      labelPlacement="center" ><!--进度条显示文字    source="{data.fileRefrence}"    progress="progress.label='%3%%';"    label="%3%%"    -->
              </mx:ProgressBar>
              <!-- 取消按钮    source="{data.fileRefrence}"
              <mx:LinkButton width="20" toolTip="取消上传" click="cancel()"
              label="取消" icon="@Embed('image/cancel.png')">
              <fx:Script>
              <![CDATA[
              private function cancel():void{
              data.fileRefrence.cancel();
              progress.label = "已取消";
              }
              ]]>
              </fx:Script>
              </mx:LinkButton>
              
-->
        
              <!--
              <mx:LinkButton width="20" click="deleteItem(event)"    
                         toolTip="从列表中删除" icon="@Embed('image/delete.png')">
                <fx:Script>
                  <![CDATA[
                    import mx.collections.ArrayCollection;
                    
                    private function deleteItem(event:MouseEvent):void {
                        
                      var s:ArrayCollection= this.parentDocument.selectedFiles as ArrayCollection;    //取全局变量
                      var index:int=s.getItemIndex(data);
                      if (index != -1){
                        s.removeItemAt(index);
                      }
                    }
                  ]]>
                </fx:Script>
              </mx:LinkButton>
              
-->
            </mx:HBox>
          </fx:Component>
        </mx:itemRenderer>
      </mx:DataGridColumn>
    </mx:columns>
  </mx:DataGrid>
  <s:Button x="523" y="373" label="上传" id="btn_upload"    lineHeight="0"    
         click="doSingleUploadFile();" />
</mx:TitleWindow>
 
文件上传服务器端(Java)
package com.test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class UploadFile extends HttpServlet {    
        private static final long serialVersionUID = 5425836142860976977L;

        /** *//**    
            * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.    
            * @param request servlet request    
            * @param response servlet response    
            */
    
         // 定义文件的上传路径    
         private String uploadPath = "d:\\";    
         // 限制文件的上传大小    
         private int maxPostSize = 100 * 1024 * 1024;    //最大100M
         public UploadFile() {    
                 super();    
         }    
         public void destroy() {    
                 super.destroy();    
         }    
         protected void processRequest(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                 System.out.println("Access !");    
                 response.setContentType("text/html;charset=UTF-8");    
                 //保存文件到服务器中    
                 DiskFileItemFactory factory = new DiskFileItemFactory();    
                 factory.setSizeThreshold(4096);    
                 ServletFileUpload upload = new ServletFileUpload(factory);    
                 upload.setHeaderEncoding("utf-8");
                 upload.setSizeMax(maxPostSize);    
                 try {    
                        List fileItems = upload.parseRequest(request);    
                        Iterator iter = fileItems.iterator();    
                        while (iter.hasNext()) {    
                                FileItem item = (FileItem) iter.next();    
                                if (!item.isFormField()) {    
                                        String name = item.getName();
                                        System.out.println(name);    
                                        try {    
                                                item.write(new File(uploadPath + name));    
                                                response.getWriter().write("上传成功。");
                                        } catch (Exception e) {    
                                                e.printStackTrace();    
                                                response.getWriter().write(e.getMessage());
                                        }    
                                }    
                        }    
                } catch (FileUploadException e) {    
                        e.printStackTrace();    
                        response.getWriter().write(e.getMessage());
                        System.out.println(e.getMessage() + "结束");    
                }    
        }    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                processRequest(request, response);    
        }    
        /** *//**    
         * Handles the HTTP <code>POST</code> method.    
         * @param request servlet request    
         * @param response servlet response    
         */
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                 processRequest(request, response);    
         }    
         /** *//**    
            * Returns a short description of the servlet.    
            */
    
         public String getServletInfo() {    
                 return "Short description";    
         }    
}    


 
由于itemRenderer的问题,文件删除和DataGrid的滚动造成进度条进度错乱的问题都没能解决.不是很满意,但至少实现了基本的多文件带进度条上传.
分享到:
评论

相关推荐

    flex带进度条上传文件

    flex带进度条上传文件,非常棒,相信你会喜欢的。大家不要下载了,这个资源不知道为什么不能用了,请csdn的管理员关注一下。

    Flex 多文件上传组件

    Flex多文件上传组件是一种在Adobe Flex环境中实现的高级功能,允许用户一次性选择并上传多个文件。这个组件在Web应用程序中非常实用,特别是在处理大量图片、文档或其他类型文件上传的场景下,大大提升了用户体验。 ...

    flex多文件上传控件(flex源码)

    在这个"flex多文件上传控件"中,我们可以看到它是Flex技术在文件上传场景中的应用,特别适合于需要批量或单个上传文件到服务器的Web应用。 该控件是通过Adobe Flash Builder 4进行开发的,Flash Builder是一个集成...

    Flex 上传文件控件 (带java服务端)

    本项目提供了一个完整的Flex文件上传控件,且集成了Java服务端处理,允许用户上传文件到服务器,并能进行多文件或单文件选择,以及设置上传文件大小限制,同时展示上传进度条,为用户提供友好的交互体验。...

    flex文件上传下载,在线以pdf形式浏览文件

    综上所述,实现"flex文件上传下载,在线以pdf形式浏览文件"这一功能,需要综合运用前端开发、后端接口设计、文件处理和安全策略等多个领域的知识,为用户提供便捷、安全的文件管理体验。在实际开发过程中,还需要...

    Flex多文件上传

    Flex多文件上传是一种在Web应用中实现用户同时上传多个文件的技术。这个小demo是使用Adobe Flash Builder 4.0作为开发环境,结合Java Servlet后端处理来完成的。Flash Builder是一款强大的集成开发环境(IDE),专门...

    FLEX多文件上传

    FLEX多文件传。带进度条的提示。

    Flex multiple file uploader (Flex多文件上传范例)

    最后,为了实现文件上传进度的显示,Flex提供了进度条组件(ProgressBar),可以绑定到FileReference的upload事件,根据上传进度更新进度条的状态。在PHP端,可以通过调整响应信息来反馈文件上传的状态,以便Flex...

    Flex多文件上传源码

    在本场景中,"Flex多文件上传源码"是指一个使用Flex技术实现的能够处理多文件同时上传的功能模块。这个功能对于Web应用程序来说非常实用,尤其是在用户需要上传大量文件时。 首先,我们要理解Flex中的组件体系。...

    flex多文件上传 asp.net实例

    本文将深入探讨一个基于Flex前端和ASP.NET后端的多文件上传实例,旨在帮助开发者理解如何实现这样的功能。 首先,Flex是一种用于创建富互联网应用程序(RIA)的开源开发框架,它基于ActionScript编程语言和Flash ...

    Flex+php 批量上传源码 (可刷新进度条 )

    以上就是使用Flex+PHP实现批量上传文件并刷新进度条的基本原理和步骤。实际应用中可能还需要根据项目需求进行调整和优化,例如添加取消上传功能、显示上传状态等。记得在编写代码时保持良好的编程习惯,以便于后期的...

    flex多文件上传

    在Flex中实现多文件上传功能是一项常见的需求,特别是在需要用户批量上传文件的Web应用中。这种功能通常涉及到异步处理,以提高用户体验,避免用户等待整个文件上传过程完成。 在Flex中实现多文件上传,主要涉及...

    flex+java文件上传

    综上所述,"flex+java文件上传"涉及到前端的Flex界面设计与交互、Flash Player运行环境、后端的Java处理逻辑以及文件上传的安全性和性能优化等多个知识点。在实际应用中,开发者需要结合这些技术来实现稳定、安全且...

    Flex文件上传组件

    Flex文件上传组件是一种基于Adobe Flex技术的用户界面组件,用于在Web应用中实现文件上传功能。Flex是一个开源的RIA(富互联网应用)框架,它允许开发者创建交互性强、图形丰富的Web应用。在这个组件中,用户可以...

    flex3 java 文件上传源码

    在文件上传场景中,Flex端通常会提供一个用户友好的界面,包括选择文件的按钮、进度条和上传状态的反馈等。以下是一些关于Flex3文件上传的关键知识点: 1. **ActionScript3**:Flex3的核心编程语言,用于编写Flex...

    FLEX上传文件,flex upload

    这里提到的“SWFUpload”是一个流行的JavaScript库,它与Flash Player结合使用,允许在浏览器中实现复杂的文件上传功能,包括多文件选择和进度条显示。虽然SWFUpload最初是一个JavaScript解决方案,但Flex开发者可以...

    Flex文件上传的组件

    综上所述,Flex文件上传组件涉及到了Flex组件设计、文件I/O操作、事件处理、HTTP通信、UI反馈等多个方面的知识,对于开发高质量的RIA应用至关重要。通过深入理解和熟练运用这些技术,开发者可以创建出高效、友好的...

    Flex+Java 文件上传

    在本文中,我们将深入探讨如何实现Flex与Java Servlet结合进行文件上传。首先,我们需要了解Flex是一种基于Adobe AIR的开源框架,用于构建富互联网应用程序(RIA),而Java Servlet是Java平台上的一个标准,用于处理...

Global site tag (gtag.js) - Google Analytics