Trying to select a files location + file name in AS3 - actionscript-3

I am creating a file testing program in actionscript 3. I have the URL loader to load the file path + name typed in the textbox. However I am looking to upgrade the experience. I am trying to find a way to choose a file location and name from a function like browseforDirectory(). If anyone could help with providing me either a link to documentation on how to do this or if anyone could help me out that would be awesome. For an example of how its set up currently.
public var TestPath:string = this.TestTxt.txt;
..
this.ldr.load(new URLRequest(TestPath));
This is pretty much a simple example of what would be going on. I am just looking for a way to find a file via browsing for it in a file select window and save the complete path of the file. Sorry if I sound repetitive and all help is appreciate. Couldn't find any documentation towards this specific topic.

I have figured out how to do it. Thank you for pointing me to browse Organis. I think explaining the use of browse a little more, or even providing a quick sample, would have proved to create an excellent answer that others who might view this topic would find helpful. Here is what I have used. It is quite crude and I will refine it later.
package {
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import flash.filesystem.File;
public class Browse extends MovieClip
{
var File1:File = new File();
public function Browse()
{
//Adds an event listener for the selected file.
File1.addEventListener(Event.SELECT, FileSelected);
//Narrows down the file selection
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.png;*.as");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
//Calls the browse function to pop up a file selection window
File1.browse([swfTypeFilter, allTypeFilter]);
}
function FileSelected(event:Event):void
{
trace(File1.nativePath);
}
}
}
Hopefully anyone looking for a way just to get a file path of a file selected can find it here.

Related

Global AS3 File - FDT

I'm trying create a global file in AS3 with FTD.
It's a file that is imported by this code
<fx:Script source="../../../framework/util/util.as"/>
and is acessible by all classes in project.
Just like this without package or class.
import flash.events.MouseEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.core.FlexGlobals;
import mx.core.UIComponent;
import mx.formatters.DateFormatter;
import mx.managers.ToolTipManager;
import mx.messaging.Channel;
import mx.messaging.events.ChannelEvent;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.utils.ObjectUtil;
import spark.components.TitleWindow;
import spark.events.TitleWindowBoundsEvent;
private var channelSet:ChannelSet = new ChannelSet();
private var customChannel:Channel;
public function showErrorImmediately(target:UIComponent):void
{
// we have to callLater this to avoid other fields that send events
// that reset the timers and prevent the errorTip ever showing up.
target.callLater(showDeferred, [target]);
}
....
In FB this file works fine but when i migrate to FDT a lot of errors occurs.
Can anyone help me?
FDT does not support the source attribute for MXML script tags:
<fx:Script source="../../../framework/util/util.as"/>
Your code in file util.as is valid if you consider it as contents of a script block.
I assume you put the file util.as in some package in your source folder.
In this case FDT assumes it is a normal compilation unit. A normal compilation unit
has to start with a package and so FDT complains about that.
In general it is a bad practice to use the include statement or the source attribute and this is why FDT does not support that.
In your case I would consider defining your own abstract mxml component as base class of all your components containing the method and the fields.

AS3 text to file

I'm making a game in cs3 using as3. I've seen dozens of tutorials, but none of them are working for me.
I found the simplest code I could, and it still gives me an error "1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference."
here's the code I'm using:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")
Are you importing FileReference? If not, you'll need:
import flash.net.FileReference;
..inside your package declaration (or at the top of your block of code if you're coding on the time line)
With that import included, your code works for me.

Slow export in Flash Professional CS6

I have linked an external library to my fla from the advanced publish settings. (It is away3d library)
There is nothing on the stage and nothing on the timeline either, I have just added some code in the Document Class(look below).
Whenever I run the code below without the line: private _view:View3D it exports fast in about a few seconds, but when I keep that line, it exports very slowly, in about 1 minute.
How can I make the export fast while using external libraries.
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.geom.Vector3D;
import away3d.primitives.PlaneGeometry;
import away3d.containers.View3D;
import away3d.entities.Mesh;
import flash.events.Event;
public class Main extends Sprite
{
private var _view:View3D;
//whenever I comment this line out, it exports fast
public function Main()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
}
}
EDIT: Here is the image of the way I linked the away3d library to my fla - http://i.imgur.com/HLxZXPi.png?1
When you link source path Flash IDE compiles all classes each time you publish your application, to prevent this link swc file (that contains away3d classes) in the Library path section.

AS3:loading XML is not working after exporting the air file?

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
names();
function names()
{
var url:URLRequest = new URLRequest("http://artbeatmedia.net/application/xmlkpisname.php?username=adel_artbeat&password=ADL1530");
url.contentType = "text/xml";
url.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.load(url);
loader.addEventListener(Event.COMPLETE, onLoadComplete);
}
function onLoadComplete(event:Event):void
{
trace('good');
}
i used adobe air to load xml using as3,this code works only in the .fla file,but when i export the .air file it does not work.
if you export a program, trace messages won't be visible.
you can still use MonsterDebugger, but thats a security hazard.
Vizzy tracer will still capture trace calls from an Air app. Get Vizzy from Google code
I tried your code with no modifications just now and it worked just fine, so I suppose your problem is just a failed trace out. Have a go, Vizzy shows even traces from within Flash IDE, so see what happens. If it still fails, then it might be some connection/permission issue.
Are you including the XML file when you publish the AiR app? Happened to me once, forgot to include the file in the publish dialogue....an hour of my life I'll never get back.

Flash saves in Windows, not in Linux, FileReference.save()

The code below compiles fine on the Flex 4 SDK on Fedora 15. Mouse-click opens the dialog box, I click okay, and a file is saved, but the file is empty. I run the same SWF file (that was compiled on the Linux machine) on a Windows machine, and the created file contains the expected data.
Then I broke the FileReference declaration out of the function into the class level, hoping to avoid a known bug, but the same problem persists.
Hoping to set up a workaround, I added the debug Flash player to my path and ran the file from Flash without the benefit of the browser, and it works. So now a Flex problem has become a Firefox problem, maybe owing to a shady procedure I used to install the plugin without really understanding what was happening. I am running Firefox 5.0.
In essence my workflow is fixed, but perhaps people who performed the above will not be able to use projects with FileReference.save()? Should I be worried about this edge case?
/*
WriteTheFile.as
Original code by Brian Hodge (brian#hodgedev.com)
Test to see if ActionScript/Flash can write files
*/
package{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.ByteArray;
import flash.net.FileReference;
public class WriteTheFile extends Sprite
{
private var _xml:String;
private var fr:FileReference;
public function WriteTheFile():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//Calling the save method requires user interaction and Flash Player 10
stage.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
}
private function _onMouseDown(e:MouseEvent):void
{
fr = new FileReference()
fr.save("<xml><test>data</test></xml>", "filename.txt");
}
}
}
EDIT: was missing a line, fixed above
EDIT: addressed answer in code above, but the same problem exists.
EDIT: This works on the same system when the standalone player is invoked. Therefore this is a browser (FF 5.0) plugin problem.
Try putting the line
var fr:FileReference = new FileReference();
at class level (outside the function). Apparently this is a known bug:
http://www.techper.net/2007/12/30/flash-filereferencebrowse-problems-on-linux/