FLEX中的多文件上载

时间:2010-08-05 12:54:25

标签: flex file flex3 upload

是否有一个FLEX组件在multipart / form-data POST请求中上传多个文件?

3 个答案:

答案 0 :(得分:1)

您可以使用FileReference(1个文件)或FileReferenceList(多个文件)来上传多个文件。

也许您可以在此页面上找到一些灵感:http://soegianto.com/blog/2008/07/multiple-file-upload-with-flex-and-php/

答案 1 :(得分:0)

如果您正在寻找使用Flex和.NET的快速示例:

<强> Uploader.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="initApp()" >

  <mx:Script>    
    <![CDATA[
      import flash.net.URLRequest;

      private var __file:FileReference;

      private function initApp():void 
      { 
        __file = new FileReference();

        __file.addEventListener(Event.SELECT                  , __fileSelectionHandler); 
        __file.addEventListener(Event.COMPLETE                , __completeHandler     );
        __file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, __uploadDataHandler   );
      }

      private function __onBrowse():void 
      {       
        try
        {    
          var success:Boolean = __file.browse();        
        }  
        catch (error:Error) 
        {
          __status__.text = "Status: Unable to open browse dialog";             
        }
      }

      private function __onUpload():void 
      {
        // replace with URL to your upload handler - THIS ONE WON'T WORK!!!!
        var myRequest:URLRequest = new URLRequest("http://<url>/Uploader.ashx");
        myRequest.method         = URLRequestMethod.POST;

        try 
        {       
          __file.upload(myRequest);         
          __status__.text = "Status: Now uploading " + __file.name + " ... ";         
        }  
        catch (error:Error) 
        {         
          __status__.text = "Status: Error uploading " + __file.name;
        }
      }

      private function __fileSelectionHandler(_e:Event):void 
      {  
        __upload__.enabled = true;   

        __uploadFile__.text = __file.name;             
        __status__.text     = "Status:  Click 'Upload' to begin file upload";
      }

      private function __completeHandler(_e:Event):void 
      {         
        // nothing currently done in this handler - experiment and have fun :)            
      }

      private function __uploadDataHandler(_e:DataEvent):void 
        {
        var myResult:XML = new XML(_e.data);
            __status__.text  = "File Upload Complete \n" + myResult.toString();
        }       
    ]]>
  </mx:Script>

  <mx:TextArea width="300" height="90" id="__status__" wordWrap="true" editable="false" enabled="true" text="Status:  Select file to upload"/>
  <mx:HBox width="300" height="20" horizontalAlign="center">
    <mx:TextInput id="__uploadFile__" editable="false" enabled="true"/>
    <mx:Button label="Browse" id="__browse__" enabled="true" click="__onBrowse()"/>
  </mx:HBox>
  <mx:HRule width="300"/>
  <mx:Button label="Upload" id="__upload__" enabled="false" click="__onUpload()"/>
</mx:Application>

<强> Uploader.ashx:

<%@ WebHandler Language="C#" Class="Uploader" %>

using System.IO;
using System.Web;
using System.Web.Configuration;

public class Uploader : IHttpHandler 
{    
  public void ProcessRequest( HttpContext _context ) 
        {
          // not very elegant - change to full path of your upload folder (there are no upload folders on my site)
    string uploadDir = "C:\\<path to upload folder>\\";

    if (_context.Request.Files.Count == 0)
    {
      _context.Response.Write("<result><status>Error</status><message>No files selected</message></result>");
      return;
    }

    foreach(string fileKey in _context.Request.Files)
    {
      HttpPostedFile file = _context.Request.Files[fileKey];
      file.SaveAs(Path.Combine(uploadDir, file.FileName));
    }

    _context.Response.Write("<result><status>Success</status><message>Upload completed</message></result>");
  }

  public bool IsReusable 
        {
    get { return true; }
  }
}

请注意,这基本上只是Algorithmist's Post from '07

的转录

答案 2 :(得分:0)

您只能使用Flex类一次上传一个。如果您想在同一个上传请求中使用多个,请尝试使用MultipartURLLoader:

http://code.google.com/p/in-spirit/wiki/MultipartURLLoader