1

このコードを使用して、選択したファイルをアップロードします。これは問題なく機能し、ファイルをアップロードした後、「uploadedFilelist」をjsonまたは文字列としてActionscriptに送信したいと思います。問題は、このデータをActionscriptに渡す方法がわからないことと、ActionScriptでこのデータを取得する方法がわからないことです。(それはASでの私の最初の経験です)

ActionScriptコード:

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 に答える