AS3上的新手! :)AS3在服務器中瀏覽SWF中的客戶端文件
基本上我想寫讓用戶選擇一個圖像文件,並顯示它(當時我會處理像素的應用程序,所以我不希望應用到圖像存儲在一個新的文件,只是管理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);
}
}
}
你是對的我沒有發佈任何代碼,這是因爲我認爲我在構建項目後錯過了一些步驟。 而你還是正確的答案,這是一個缺乏互動 我編輯的問題添加的代碼:),也許你可以指出一些點是代碼可以改進/可導致快速失敗 謝謝! :) – datamosh
儘管index.html可以在firefox中正常工作,但它並不總是與chrome一起工作一到兩次......重新啓動瀏覽器後也是如此。 – datamosh
爲你更新 –