1 Reply Latest reply: Dec 20, 2012 5:54 AM by mreuter RSS

    How to use getRelativePath() with browseForOpen()? It returns null

    Raestloz Community Member

      It's quite simple:

       

      fTargetFile:File = File.applicationDirectory.resolvePath("assets");

      fRootFile:File = File.applicationDirectory.resolvePath("assets");

       

      fTargetFile.addEventListener(Event.SELECT,onSelect)

      fTargetFile.browseForOpen("test"); <-- here I open a file in "/assets/images/test.jpg"

       

      function onSelect(e:Event):void

      {

          trace(fRootFile.getRelativePath(fTargetFile));  <-- this returns null

      }

       

      Every single "tutorial" in the web - along with Adobe's documentation - gracefully uses resolvePath to explicitly point to the file objects, thereby bypassing the actual use case. getRelativePath indeed works with resolvePath, but using resolvePath means I've already known where the file is located in the first place, rendering getRelativePath actually useless

       

      tracing the nativePath property of both objects returns the correct path.

       

       

      What I'm trying to do is letting the user choose a file within the "assets" directory. The file can be from any folder so long as they're under the "assets", and once user chooses a file, display the relative path from "assets" to the chosen file.

       

      So, in the example above, I expect "images/test.jpg" to be returned, but it returns nothing

       

      Do I have to do some wizardry before getRelativePath works?

        • 1. Re: How to use getRelativePath() with browseForOpen()? It returns null
          mreuter Community Member

          This post is rather old, so I may not be able to help the original author of this topic, but as I found this thread, when searching for a solution for this problem myself, I might be able to help someone else.

           

          I found the following solution:

           

          function onSelect(e:Event):void

          {

            var f1:File = (new File()).resolvePath(e.currentTarget.nativePath);

            var f2:File = (new File()).resolvePath(File.applicationDirectory.nativePath);

            trace(f2.getRelativePath(f1, true));

          }