AS3上的新手在这里! :)
基本上,我正在尝试编写一个应用程序,让用户选择一个图像文件并显示它(然后我将操作像素,所以我不希望应用程序将图像存储在新文件中,只是管理 ByteArray)。
到目前为止,我在Flash中编写了一些工作代码,这些代码显示一个窗口来选择图像,然后显示它。 但是当我将生成的文件(myapplication.swf,expressinstall.swf,index.html和js文件夹)上传到服务器时,窗口不再显示。
我正在使用FileReference.browse()方法。
怎么了?
(编辑:正如The_asMan指出的那样,我们在这里错过了一些代码,这里是 - 根据The_asMan的建议进行了改进)
我的包裹:
package searchfiles
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.display.DisplayObject;
/**
* ...
* @author ddd
*/
public class searchForFiles extends EventDispatcher
{
public var newfile:FileReference;
public var loader:Loader
public var bitmapimg:BitmapData;
public function searchForFiles() {
newfile = new FileReference();
newfile.addEventListener(Event.SELECT, onFileSelected);
newfile.addEventListener(Event.CANCEL, onCancel);
newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
trace("abbiamo instanziato un searchForFiles");
var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)",
"*.png; *.jpg; *tif");
newfile.browse([textTypeFilter]);
}
public function onFileSelected(evt:Event):void
{
newfile.addEventListener(ProgressEvent.PROGRESS, onProgress);
newfile.addEventListener(Event.COMPLETE, onComplete);
newfile.load();
}
public function onProgress(evt:ProgressEvent):void
{
trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
}
public function onComplete(evt:Event):void
{
trace("File was successfully loaded.");
loader = new Loader();
loader.loadBytes(newfile.data);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
}
private function erroremanip(evt:IOErrorEvent):void {
trace("errore " + evt);
}
private var bitmapData:BitmapData
public function getBitmapData(e:Event):void {
var content:* = loader.content;
bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
trace("loader.width = " +loader.width);
dispatchEvent( new Event(Event.COMPLETE));
//trace("get bitmap data called");
}
public function onCancel(evt:Event):void
{
trace("The browse request was canceled by the user.");
}
public function onIOError(evt:IOErrorEvent):void
{
trace("There was an IO Error.");
}
public function onSecurityError(evt:Event):void
{
trace("There was a security error.");
}
}
}
这是主要()
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import searchfiles.searchForFiles;
/**
* ...
* @author ddd
*/
[SWF(width = "550", height = "600")]
public class Main extends MovieClip
{
public var file:searchForFiles;
public var mybtn:Loader = new Loader();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
var urlqst:URLRequest = new URLRequest("preview_true.png");
mybtn.load(urlqst);
addChild(mybtn);
}
public function mouseclicked(e:MouseEvent):void {
trace("clicked");
file = new searchForFiles();
file.addEventListener(Event.COMPLETE, puttheimage);
}
private function erroremanip(e:IOError):void {
trace("ciao erroreio");
}
private function puttheimage(e:Event) :void {
addChild(file.loader);
}
}
}
FileReference.browse() 当外部沙箱需要通过用户交互触发时:鼠标单击。
基本上,点击事件需要在堆栈中的某个地方。
您可以通过以下方式验证这一点。
file = new FileReference();
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
但是,您没有发布任何代码,很难确切地确定您做错了什么。
[编辑]
package searchfiles
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.FileFilter;
import flash.events.*;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.display.DisplayObject;
/**
* ...
* @author ddd
*/
public class searchForFiles extends EventDispatcher
{
public var newfile:FileReference;
public var loader:Loader
public var bitmapimg:BitmapData;
public function searchForFiles() {
newfile = new FileReference();
newfile.addEventListener(Event.SELECT, onFileSelected);
newfile.addEventListener(Event.CANCEL, onCancel);
newfile.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
newfile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
}
// new function
public function browse(event:Event):void{
var textTypeFilter:FileFilter = new FileFilter("Image files (*.png, *.jpg, *tif)", "*.png; *.jpg; *tif");
newfile.browse([textTypeFilter]);
}
public function onFileSelected(evt:Event):void
{
newfile.addEventListener(ProgressEvent.PROGRESS, onProgress);
newfile.addEventListener(Event.COMPLETE, onComplete);
newfile.load();
}
public function onProgress(evt:ProgressEvent):void
{
trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
}
public function onComplete(evt:Event):void
{
trace("File was successfully loaded.");
loader = new Loader();
loader.loadBytes(newfile.data);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, getBitmapData);
}
private function erroremanip(evt:IOErrorEvent):void {
trace("errore " + evt);
}
private var bitmapData:BitmapData
public function getBitmapData(e:Event):void {
var content:* = loader.content;
bitmapData = new BitmapData(content.width,content.height,true,0x00000000);
trace("loader.width = " +loader.width);
dispatchEvent( new Event(Event.COMPLETE));
//trace("get bitmap data called");
}
public function onCancel(evt:Event):void
{
trace("The browse request was canceled by the user.");
}
public function onIOError(evt:IOErrorEvent):void
{
trace("There was an IO Error.");
}
public function onSecurityError(evt:Event):void
{
trace("There was a security error.");
}
}
}
这是主要()
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.errors.IOError;
import flash.events.*;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import searchfiles.searchForFiles;
/**
* ...
* @author ddd
*/
[SWF(width = "550", height = "600")]
public class Main extends MovieClip
{
public var file:searchForFiles;
public var mybtn:Loader = new Loader();
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
// moved to init
file = new searchForFiles();
file.addEventListener(Event.COMPLETE, puttheimage);
mybtn.addEventListener(MouseEvent.CLICK, mouseclicked);
mybtn.addEventListener(IOErrorEvent.IO_ERROR, erroremanip);
var urlqst:URLRequest = new URLRequest("preview_true.png");
mybtn.load(urlqst);
addChild(mybtn);
}
public function mouseclicked(e:MouseEvent):void {
trace("clicked");
// events need to be set before any active code is run in the object
// that is why we moved listeners or else you risk the listener
// not getting triggered
file.browse()
}
private function erroremanip(e:IOError):void {
trace("ciao erroreio");
}
private function puttheimage(e:Event) :void {
addChild(file.loader);
}
}
}
FileReference
用于访问用户本地计算机上的文件。听起来您想从托管 SWF 文件的同一服务器加载文件。
您无法从 Actionscript "浏览"服务器 - 除非您在服务器上编写代码以启用它 - 但您可以使用 URLLoader
按名称加载文件。