2012-09-23 155 views
1

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); 

     } 
    } 
} 

回答

0

FileReference.browse() 當需要通過用戶交互IE觸發外部本地沙箱時:鼠標點擊。
基本上,點擊事件需要放在某處。
您可以使用驗證。

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); 

     } 
    } 
} 
+0

你是對的我沒有發佈任何代碼,這是因爲我認爲我在構建項目後錯過了一些步驟。 而你還是正確的答案,這是一個缺乏互動 我編輯的問題添加的代碼:),也許你可以指出一些點是代碼可以改進/可導致快速失敗 謝謝! :) – datamosh

+0

儘管index.html可以在firefox中正常工作,但它並不總是與chrome一起工作一到兩次......重新啓動瀏覽器後也是如此。 – datamosh

+0

爲你更新 –

0

FileReference用於訪問用戶本地機器上的文件。這聽起來像您想要從承載SWF文件的同一服務器加載文件。

您不能從Actionscript瀏覽服務器 - 除非您在服務器上編寫代碼以啓用該功能 - 但您可以使用URLLoader按名稱加載文件。

+0

嗨,彼得,謝謝您的回答,但我確實想訪問用戶的本地機器上的文件! 所以我正在使用FileReference在正確的方向。 也許是與某種權限有關的東西? – datamosh

相關問題