另一个与QUOT;在ActionScript的3英寸本地文件访问;题 [英] Yet another "local file access in actionscript-3" q-u-e-stion

查看:290
本文介绍了另一个与QUOT;在ActionScript的3英寸本地文件访问;题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我能得到一些帮助,我的动作的问题。

I hope I can get some help with my actionscript problem.

(inb4:?究竟是什么你想完成(除未经用户许可打开任意文件)链接 的)

(inb4: 'What exactly are you trying to accomplish (other than opening an arbitrary file without user permission)?' link)

我试图帮助学生练习 - 所以我想我会拿出一个随机画廊的一个例子。这样做的想法是,学生将能够产生一个SWF文件,并​​在同一目录下,他们将有图像的文件夹。然后,SWF动画会读取目录的内容;选择一个随机图像,并显示出来。

I am trying to assist with student exercises - so I thought I'd come up with an example of a random gallery. The idea with this would be that students would be able to generate a SWF file, and in the same directory, they would have a folder with images. Then the SWF movie would read the contents of the directory; choose a random image, and display it.

我本来希望能够做到这一点无论在本地(而学生发展) - 和,他们应该选择的话,他们应该能够上传整个目录在线 - 与希望,这个例子将工作无论哪种方式。这证明不是那么容易 - 所以这些都是我的问题

I would have hoped that this could be done both locally (while students develop) - and, should they choose so, they should be able to upload the entire directory online -- and hopefully, the example would work either way. That turns out not to be so easy - so these are my questions:

  • 他们说,是为了获得本地目录列表,我应该用AIR - 这是什么意思?有一些可执行文件 - 的比 flashplayerdebugger - 我应该为了重现SWF如果它使用AIR打电话?
    • 是否有可能得到一个本地目录列表不使用此AIR(在此类似如下的Flex的code?)
    • They say, that in order to get local directory listing, I should use AIR - what does that mean? Is there some executable - other than flashplayerdebugger - that I should call in order to reproduce the SWF if it uses AIR?
      • Is it possible to get a local directory listing without using this AIR (in the similar vein as the Flex code below?)

      现在,有一件事是,大多数学生的工作与Flash CSX版本 - 在另一方面上午在Linux上我,所以我试图做一个Flex例子;所以我提供的例子作为Flex的code,但我想知道可能的答案是Flash CS环境了。目录结构,编译线和code给出在这篇文章结束 - 但这里是怎样的应用程序目录如下:

      Now, one thing is that most students work with Flash CSx versions - I on the other hand am on Linux, so I have tried to make a Flex example; so I've provided the example as Flex code, but I'd like to know possible answers for the Flash CS environment too. The directory structure, compile lines, and code is given at end of this post - but here is how the "application" directory looks like:

      好了,所以这里是一个更详细一点的问题。首先,我想我会尝试读取目录内容 - 所以我尝试了code在这里:的 AS3基础知识:列出包括子目录(AIR ... 对学生的Flash CS文件夹中的所有文件 - 但似乎失败草草收场。

      Ok, so here is the problem in a bit more detail. First, I thought I'd try to read the directory contents - so I tried the code here: AS3 Basics: List all files in a folder including subdirectories (AIR ... on students' Flash CS - but that seemed to fail miserably.

      所以我想 - 好吧,那么让我们把在一个文本文件在同一目录瑞士法郎,其中将包含图像的列表;例如,

      So I thought - OK, then let's put in a text file in the same directory as the .swf, which will contain a list of the images; e.g.

      cat > fileslist.txt <<EOF
      img/01.jpg
      img/02.jpg
      EOF
      

      随后,Flash应用程序会首先读取这个文件 - ,然后进行选择的内容的随机图像。但在第一个瞬间,我得到:

      Then, the Flash app would read this file first - and then proceed to choose a random image from the contents. But at the very first instant, I get:

      An ActionScript error has occurred:
      SecurityError: Error #2148: SWF file file:///home/rgaltest/rgaltest.swf cannot access local resource file:///home/rgaltest/fileslist.txt. Only local-with-filesystem and trusted local SWF files may access local resources.
        at flash.net::URLStream/load()
        at flash.net::URLLoader/load()
        at rgaltest()[/home/rgaltest/rgaltest.as:23]
      

      所以,这个问题的答案似乎是:

      So, the answer to this seems to be:

      • "If you are getting that error when using flash.net.URLLoader and the free flex compiler, add the following option when you compile to get your swf to work: -use-network=false" (juicy tidbits - Only local-with-filesystem and trusted local SWF files may access local resources.)
      • "Turns out the solution is to configure your Flash Player to trust the swfs in the project’s /bin-debug directory ... 1) Goto http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html ... " (Solution – Error #2010: Local-with-filesystem SWF files are not permitted to use sockets. » Mark Lapasa)

      现在,我的不可以喜欢有 -use网络= FALSE - 因为学生可能需要集成其他基于网络的东西在那例。再说,类似的东西 settings_manager04.html 全局安全性设置面板的)页面显示,如果你在一个打开的SWF浏览器,然后单击鼠标右键,然后选择全局设置 - 但它的SWF文件的工作被显示为Ctrl键,回车在Flash中构建CS(或相应地,那些开放与 flashplayerdebugger的一部分)所以我尝试 - 选择编辑位置,浏览文件夹,选择 /主页/ rgaltest 下的总是允许?

      Now, I would not like to have -use-network=false - because students may need to integrate other network based stuff in that example. Then again, something similar to that "settings_manager04.html" ("Global Security Settings panel") page shows up if you open the SWF in a browser, then right-click, then choose "Global Settings" - but does it work for SWF files being shown as a part of Ctrl-Enter build in Flash CS (or correspondingly, those that are open with flashplayerdebugger?) So I try it - choose "Edit Locations", "Browse for folder", choose /home/rgaltest under "Always Allow":

      ...现在 fileslist.txt 被加载好了;还当瑞士法郎是跑了 flashplayerdebugger

      ... and now the fileslist.txt gets loaded alright; also when the .swf is ran with flashplayerdebugger.

      对,所以现在的文件列表加载时,我会继续在加载位图..所以我添加了相应的code(带 onImgLoaded 等),这就是我得到的日志中:

      Right, so now that the files list is loaded, I'd proceed on loading the bitmaps.. so I add the respective code (with onImgLoaded etc), and this is what I get in the log:

      At frame #1
      file onTextLoaded: img/01.jpg,img/02.jpg,
      At frame #2
      file onImgLoaded: [object URLLoader]
      TypeError: Error #1034: Type Coercion failed: cannot convert "ÿØÿà
      ...
      ¿ÿÙ" to flash.display.Bitmap.
        at rgaltest/onImgLoaded()[/home/rgaltest/rgaltest.as:64]
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at flash.net::URLLoader/onComplete()
      At frame #3
      At frame #4
      ...
      

      有趣的是 - 在图像的没有获取读 - 因为所有的二进制内容转储到日志!然而,它不能PTED作为位图数据间$ P $ ???!

      Interestingly - the image did get read - as all of its binary contents are dumped to the log! Yet, it cannot be interpreted as bitmap data???!

      首先,这是我的目录布局:

      First of all, this is my directory layout:

      mkdir /home/rgaltest
      cd /home/rgaltest
      touch rgaltest.as
      touch fileslist.txt
      mkdir img
      cd img
      convert -size 320x240 gradient:\#4b4-\#bfb 01.jpg
      convert -size 320x240 gradient:\#b44-\#bff 02.jpg
      chmod -R 777 /home/rgaltest   # to ensure no problem with file permissions
      

      rgaltest.as 下面给出;我编译使用:

      The rgaltest.as is given below; I compile that with:

      /path/to/flex_sdk_4.6.0.23201_mpl/bin/mxmlc -static-link-runtime-shared-libraries -use-network=true -verbose-stacktraces -debug=true rgaltest.as
      

      ...我检查与本地(确保的mm.cfg 正确设置,也见的使用Flash Player调试 的):

      ... and I check that locally with (make sure mm.cfg is properly set up, see also Using Flash Player debugger):

      # in one terminal:
      tail -f ~/.macromedia/Flash_Player/Logs/flashlog.txt
      
      # in another terminal:
      /path/to/flashplayer_11/flashplayerdebugger rgaltest.swf
      

      &NBSP;

       

      最后,这里是 rgaltest.as

      package {
        import flash.display.Sprite;
        import flash.text.TextField;
          import flash.display.MovieClip;
          import flash.events.Event;
          import flash.events.IEventDispatcher;
          import flash.net.*; //URLLoader;
          import flash.display.Bitmap;
          import flash.display.BitmapData;
      
          [SWF(width="550", height="400")]
          public class rgaltest extends MovieClip {
      
          public var _pictureArray:Array;
          public var frameCnt:Number = 0;
      
          public var myTextLoader:URLLoader;
          public var isFileslistLoaded:Boolean = false;
      
          public var myImgLoader:URLLoader;
          public var isImgLoading:Boolean = false;
      
      
              // constructor - create/initialize objects here
              public function rgaltest() {
            this.stage.frameRate = 10; // frames per second
      
            // load the text file with image list
            myTextLoader = new URLLoader();
            myTextLoader.addEventListener(Event.COMPLETE, onTextLoaded);
            myTextLoader.load(new URLRequest("fileslist.txt"));
      
                  // add event listener for onEnterFrame
                  configureListeners(this);
          }
      
          private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.ENTER_FRAME, my_OnEnterFrame);
              }
      
          public function onTextLoaded(e:Event):void {
            _pictureArray = e.target.data.split(/\n/);
            trace("file onTextLoaded: " + _pictureArray);
            isFileslistLoaded = true;
          }
      
              public function my_OnEnterFrame(event:Event):void {
            frameCnt++;
            trace("At frame #" + frameCnt);
            if (isFileslistLoaded) {
              if (!isImgLoading) {
                //load a random image
                var rndChoice:Number = Math.floor(Math.random()*2) + 1;
                myImgLoader = new URLLoader();
                myImgLoader.addEventListener(Event.COMPLETE, onImgLoaded);
                myImgLoader.load(new URLRequest(_pictureArray[rndChoice]));
                isImgLoading = true;
              }
            }
          } // end onEnterFrame
      
          public function onImgLoaded(e:Event):void {
            trace("file onImgLoaded: " + e.target);
            var _image:Bitmap = Bitmap(e.target.data);
            var _bitmap:BitmapData = _image.bitmapData;
            addChild(_image);
          }
      
        } //end class
      } //end package
      


      其他一些相关的链接:


      Some other relevant links:

      • <一个href="http://stackoverflow.com/questions/2346547/adding-local-filenames-references-to-array-in-actionscript">flex - 添加本地文件名/引用数组在ActionScript - 堆栈溢出
      • <一个href="http://stackoverflow.com/questions/7450888/is-there-a-way-to-read-local-files-in-actionscript-3-silently">flash - 有没有办法读取ActionScript 3中的本地文件悄无声息? - 堆栈溢出
      • <一个href="http://stackoverflow.com/questions/2155021/adobe-flash-cs4-as3-get-current-directory-of-a-running-project">adobe闪光CS4 AS3:得到一个正在运行的项目的当前目录? - 堆栈溢出
      • 弯曲 - 在AS3中打开本地文件 - 堆栈溢出
      • flex - Adding Local Filenames/References To Array In Actionscript - Stack Overflow
      • flash - Is there a way to read local files in ActionScript 3 "silently"'? - Stack Overflow
      • adobe flash cs4 as3: get current directory of a running project? - Stack Overflow
      • flex - open local files in AS3 - Stack Overflow

      推荐答案

      凹凸 - 看来我解决了位图加载(将在这里单独的职位,这样我就不会堵塞OP太多);我只是用装载机类代替的URLLoader ;见下文code(也注意到这个错误随机计算OP,这是纠正以下的)。

      Bump - it seems I solved the bitmap loading (putting it here in separate post, so that I don't clog the OP too much); I just used Loader class instead of URLLoader; see code below (also note the mistake in random calculation in OP, which is corrected below).

      不管怎样 - 这就是事情:当我尝试了装载机班对学生的Flash CSX,它往往惨败!不过,也许这是因为在这种情况下,我们没有给任何权限从 settings_manager04.html 的文件夹?应采取照顾,当code与此类似闪光灯产生的任何特殊的东西?

      Anyways - this is the thing: when I tried the Loader class on the students' Flash CSx, it tended to fail miserably! But, maybe that was because in those cases, we didn't give any permissions to the folder from "settings_manager04.html"? Any special things that should be taken care of, when a code similar to this is generated in Flash?

      下面是返工 rgaltest.as

      package {
        import flash.display.Sprite;
        import flash.text.TextField;
        import flash.display.MovieClip;
        import flash.events.Event;
        import flash.events.IEventDispatcher;
        import flash.net.*; //URLLoader;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.Loader; //*
      
        [SWF(width="550", height="400")]
        public class rgaltest extends MovieClip {
      
          public var _pictureArray:Array;
          public var frameCnt:Number = 0;
      
          public var myTextLoader:URLLoader;
          public var isFileslistLoaded:Boolean = false;
      
          public var myImgLoader:Loader;
          public var isImgLoading:Boolean = false;
      
      
          // constructor - create/initialize objects here
          public function rgaltest() {
            this.stage.frameRate = 10; // frames per second
      
            // load the text file with image list
            myTextLoader = new URLLoader();
            myTextLoader.addEventListener(Event.COMPLETE, onTextLoaded);
            myTextLoader.load(new URLRequest("fileslist.txt"));
      
            // add event listener for onEnterFrame
            configureListeners(this);
          }
      
          private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.ENTER_FRAME, my_OnEnterFrame);
          }
      
          public function onTextLoaded(e:Event):void {
            _pictureArray = e.target.data.split(/\n/);
            trace("file onTextLoaded: " + _pictureArray);
            isFileslistLoaded = true;
          }
      
          public function my_OnEnterFrame(event:Event):void {
            frameCnt++;
            trace("At frame #" + frameCnt);
            if (isFileslistLoaded) {
              if (!isImgLoading) {
                //load a random image
                var rndChoice:Number = Math.floor(Math.random()*2) + 0;
                trace("Loading: " + rndChoice + " / " + _pictureArray[rndChoice]);
                myImgLoader = new Loader();
                myImgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded);
                myImgLoader.load(new URLRequest(_pictureArray[rndChoice]));
      
                isImgLoading = true;
              }
            }
          } // end onEnterFrame
      
          public function onImgLoaded(e:Event):void {
            trace("file onImgLoaded: " + e.target);
            var _image:Bitmap = Bitmap(myImgLoader.content);
            var _bitmap:BitmapData = _image.bitmapData;
            addChild(_image);
          }
      
        } //end class
      } //end package
      

      这篇关于另一个与QUOT;在ActionScript的3英寸本地文件访问;题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆