ActionScript 如何从 ASHX 获取数据
Posted
技术标签:
【中文标题】ActionScript 如何从 ASHX 获取数据【英文标题】:How ActionScript Can Get Data From ASHX 【发布时间】:2012-08-24 00:45:17 【问题描述】:使用此代码,我上传选定的文件。这没有问题,在上传文件后,我想将“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));
【问题讨论】:
【参考方案1】:这很简单。您只需添加DataEvent.UPLOAD_COMPLETE_DATA
事件侦听器并获取数据。
fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
private function uploadCompleteHandler(event:DataEvent):void
trace(event.data); //response data
【讨论】:
@peyman gilmour,不客气。顺便说一句,您可以接受答案;)以上是关于ActionScript 如何从 ASHX 获取数据的主要内容,如果未能解决你的问题,请参考以下文章
将变量从 Html StageWebview 传递到 actionscript
ActionScript3:在一个类中通过 addEventListener 读取文件,如何将数据传递给不同的类