1

使用此代码,我上传选定的 Files 。这没有问题,在上传文件后,我想将“uploadedFilelist”作为 json 或字符串发送到 Actionscript 。问题是我不知道如何将这些数据传递给 Actionscript,而且我也不知道如何在 ActionScript 中获取这些数据。(这是我第一次接触 AS )

动作脚本代码:

package 
{
import flash.external.*;
import flash.net.FileReferenceList;
import flash.net.FileReference;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.system.Security;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

public class MultiSelectClass extends MovieClip
{
    private var fileRefList:FileReferenceList=new FileReferenceList();
    private var myRequest:URLRequest; 
    private var serviceUrl:String ="";
    private var metaData:String="";

    function MultiSelectClass()
    {
        Security.allowDomain("*");

        fileRefList.addEventListener(Event.SELECT,selectHandler);
        fileRefList.addEventListener(Event.COMPLETE,completeHandler);
        myButton.addEventListener(MouseEvent.CLICK,myButtonClick);

        serviceUrl=ExternalInterface.call("MultiFileUploaderFunc.GetServiceUrl");
        myRequest = new URLRequest(serviceUrl);

        metaData = ExternalInterface.call("MultiFileUploaderFunc.GetMetdaData")         
    }

    function myButtonClick(ev:MouseEvent):void
    {
        var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
        var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
        fileRefList.browse([swfTypeFilter,allTypeFilter]);
    }

    function selectHandler(e:Event):void
    {
        var vars:URLVariables = new URLVariables();
        myRequest.method = URLRequestMethod.POST;

        for each (var file:FileReference in fileRefList.fileList)
        {
            file.addEventListener(Event.COMPLETE, completeHandler);
            file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);

            vars.fileData = file.data;
            vars.metaData= metaData;
            myRequest.data = vars;

            file.upload( myRequest);
        }
    }

    function completeHandler(e:Event):void
    {                       
        ExternalInterface.call("alert","Ok!");
    }

    private function securityErrorHandler(event:SecurityErrorEvent):void
    {
        ExternalInterface.call("alert","Security Problem !");
    }

    private function ioErrorHandler(event:IOErrorEvent):void
    {
        ExternalInterface.call("alert","I/O Problem !");
    }
}
}

通用处理程序代码:

 public class Handler1 : IHttpHandler
 {

public void ProcessRequest(HttpContext context)
{
  if (context.Request.Params["metaData"] == string.Empty)
    return;

  var metaData = context.Request.Params["metaData"];

  var jss = new JavaScriptSerializer();

  var metadataDictionary = jss.Deserialize<dynamic>(metaData);

  var jsFileType = metadataDictionary["FileType"];

  var uploadedFilelist = new List<FileModel>();

  foreach (string file in context.Request.Files)
  {
    var httpFile = context.Request.Files[file];

    if (httpFile.ContentLength == 0)
      continue;

    string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,   "Upload\\", Path.GetFileName(httpFile.FileName));
    httpFile.SaveAs(savedFileName);


    var uploadedFile = new FileModel
    {
      Extension = System.IO.Path.GetExtension(httpFile.FileName),
      Id = ID,
      MimeType = httpFile.ContentType,
      Name = httpFile.FileName,
      FileType = jsFileType
    };

    uploadedFilelist.Add(uploadedFile);
  }

  context.Response.ContentType = "application/json";
  context.Response.Write(jss.Serialize(uploadedFilelist));

}
4

1 回答 1

1

这很简单。您只需添加DataEvent.UPLOAD_COMPLETE_DATA事件侦听器并获取数据。

fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);

private function uploadCompleteHandler(event:DataEvent):void {
    trace(event.data); //response data
}
于 2012-08-29T12:34:11.333 回答