何时/如何初始化 URL 请求
我做了一个图像上传管理器。我最初是在 Flash Develop 中将其作为 AS 类制作的。我需要将其转换为 Flash Builder 4.5 中的组件。它作为 .swf 工作得非常好,但我不知道如何使 URL 请求在 Flash Builder 中工作。这是我在标签之间的内容:
<?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/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<fx:Script>
<![CDATA[
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.events.TimerEvent;
public var file:FileReference;
public var filefilters:Array;
public var req:URLRequest;
public var tm:Timer;
public var speed:Number = 0;
public var currbytes:Number = 0;
public var lastbytes:Number = 0;
public function init():void{
req = new URLRequest();
req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://www.listgiant.com/LG/upload.php';
file = new FileReference();
setup( file );
select_btn.addEventListener( MouseEvent.CLICK, browse );
tm = new Timer( 1000 );
tm.addEventListener( TimerEvent.TIMER, updateSpeed );
}
public function browse( e:MouseEvent ):void{
filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
file.browse( filefilters );
}
private function setup( file:FileReference ):void{
file.addEventListener( IOErrorEvent.IO_ERROR, io_error );
file.addEventListener( Event.OPEN, open_func );
file.addEventListener( Event.SELECT, selectHandler );
file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );
}
private function io_error( e:IOErrorEvent ):void{
label_txt.text = 'The file could not be uploaded.';
tm.stop();
}
private function open_func( e:Event ):void{
tm.start();
}
private function selectHandler( e:Event ):void{
file.upload( req );
}
private function show_message( e:DataEvent ):void{
tm.stop();
if( e.data == 'ok' ){
label_txt.text = 'The file has been uploaded.';
} else if( e.data == 'error'){
label_txt.text = 'The file could not be uploaded.';
}
}
private function updateSpeed( e:TimerEvent ):void{
speed = Math.round( (currbytes - lastbytes)/1024 );
lastbytes = currbytes;
}
private function cancelUpload( e:MouseEvent ):void{
file.cancel();
reset();
}
private function reset():void{
select_btn.visible = true;
label_txt.text = '';
}
]]>
</fx:Script>
<s:Button id="select_btn" label="Upload" click="browse(event)"/>
<s:Label id="label_txt" text=""/>
我没有放置 mxml 控件,但在显示各种状态消息的按钮下有一个浏览按钮 (id="selects_btn") 和一个标签 (id="label_txt")。
我尝试将 init 函数添加到组件的creationComplete 事件中。我收到错误消息说访问空对象。
I have made an image upload manager. I made it initially in Flash Develop as an AS class. I need to convert it to a component in Flash Builder 4.5 It works absolutely fine as a .swf, but I can't figure out how to make the URL request work in Flash Builder. This is what I have between the tags:
<?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/mx" minWidth="955" minHeight="600"
creationComplete="init()">
<fx:Script>
<![CDATA[
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.net.URLRequest;
import flash.utils.Timer;
import flash.events.TimerEvent;
public var file:FileReference;
public var filefilters:Array;
public var req:URLRequest;
public var tm:Timer;
public var speed:Number = 0;
public var currbytes:Number = 0;
public var lastbytes:Number = 0;
public function init():void{
req = new URLRequest();
req.url = ( stage.loaderInfo.parameters.f )? stage.loaderInfo.parameters.f : 'http://www.listgiant.com/LG/upload.php';
file = new FileReference();
setup( file );
select_btn.addEventListener( MouseEvent.CLICK, browse );
tm = new Timer( 1000 );
tm.addEventListener( TimerEvent.TIMER, updateSpeed );
}
public function browse( e:MouseEvent ):void{
filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
file.browse( filefilters );
}
private function setup( file:FileReference ):void{
file.addEventListener( IOErrorEvent.IO_ERROR, io_error );
file.addEventListener( Event.OPEN, open_func );
file.addEventListener( Event.SELECT, selectHandler );
file.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, show_message );
}
private function io_error( e:IOErrorEvent ):void{
label_txt.text = 'The file could not be uploaded.';
tm.stop();
}
private function open_func( e:Event ):void{
tm.start();
}
private function selectHandler( e:Event ):void{
file.upload( req );
}
private function show_message( e:DataEvent ):void{
tm.stop();
if( e.data == 'ok' ){
label_txt.text = 'The file has been uploaded.';
} else if( e.data == 'error'){
label_txt.text = 'The file could not be uploaded.';
}
}
private function updateSpeed( e:TimerEvent ):void{
speed = Math.round( (currbytes - lastbytes)/1024 );
lastbytes = currbytes;
}
private function cancelUpload( e:MouseEvent ):void{
file.cancel();
reset();
}
private function reset():void{
select_btn.visible = true;
label_txt.text = '';
}
]]>
</fx:Script>
<s:Button id="select_btn" label="Upload" click="browse(event)"/>
<s:Label id="label_txt" text=""/>
I didn't put the mxml controls but there is a browse button (id="selects_btn") and a label (id="label_txt") under the button that displays various status messages.
I tried adding the init function to the component's creationComplete event. I receive and error saying access of a null object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许声明中添加
stage
对象为 null。在applicationComplete
属性并将值设置为init()
方法,如下所示:Maybe the
stage
object is null. In the<s:Application>
declaration add theapplicationComplete
attribute and set the value to theinit()
method so it looks like the following:没有行号,我只能猜测,但也许
select_btn
在这一行为空:您可以将事件侦听器放在 btn 本身上:
Without a line number, I can only guess, but maybe
select_btn
is null at this line:You could put the event listener on the btn itself: