url request application storage directory AS3 Air - actionscript-3

I have an application prepared in Action Script 3. There is a part related to requesting a file from application storage directory. However when I use following codes (xmlLoader part), program looks into app directory.
How can I make it look into application storage directory or may any other code alternatives be used for this request? Thanks.
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest(Puzzleoriginalpath + ".xml"));

You can get the storage directory through
File.applicationStorageDirectory.nativePath
Then build your path:
File.applicationStorageDirectory.nativePath + "/examplepuzzle.xml"

Related

AngularJS: simulate URL click and rename file to be downloaded

I have this dashboard (in angularjs) where you can see some files you uploaded through a custom service. In the interface, every object has its own file_id property. The service I use can generate an URL to download the file based on this id, but it's available only for 15 minutes (which is an inconvenient, but I have to work with it).
What I did was the following: when I click on the Download button in the interface, I make a call in the back-end to a service that generates a new URL (every time) and then sends it to front-end.
Here, I am trying to simulate a click on that link with the following code:
var downloadLink = document.createElement('a');
document.body.appendChild(downloadLink);
downloadLink.setAttribute('download', 'result.' + fileExtension);
downloadLink.setAttribute('href', downloadURI);
downloadLink.click();
The download it's working, but the filename is not the desired one (it's a random string - actually a hash that service is using - without an extension) and that can trick the inexperienced users.
Can I do something regarding this? Is there a better method to achieve this?
Thanks!

AS3 write string to an existing text file

I'm running my program on AIR. I want my game to save the high score to a text file so it can be stored when the program is closed. I've tried using filestreams, however I've found that the application directory is read only. Is there a better way to do this?
The AIR application storage directory is designed for saving these sorts of preferences and user settings:
For every AIR application, there is a unique associated path that
defines the application storage directory. This directory is unique to
each application and user. You can use this directory to store
user-specific, application-specific data (such as user data or
preferences files).
Access it through the File class:
var file:File = File.applicationStorageDirectory;
file = file.resolvePath("prefs.xml");
See also: Reading from and writing to an XML preferences file
The only caveat I have found with using this directory is that it does not get removed when the AIR app is uninstalled using the .air installer/uninstaller.

Forcing SWF to load file in local driver of server before sending over browser

I am building a flash application which requires loading of an xml file using URLLoader. While developing application in my local machine with flash professional I can easily load it by
private var myLoader:URLLoader = new URLLoader(new URLRequest("./com/assets/config.xml"));
When I publish the application and click on the html generated and the app loads on browser perfectly.
If I make a server (localhost:1111) that delivers the html file over browser on connect, the html file doesn't load the application (.swf).
While trying to debug it, I found that if I change the myLoader variable as below, the html file loads the swf properly.
private var myLoader:URLLoader = new URLLoader(new URLRequest("http://localhost:1111/com/assets/config.xml"));
I believe the SWF is making another GET request after the html loads on my browser, that is the reason the SWF doesn't work without the change.
Is there any way I can load the xml file in SWF before it gets delivered over browser. This is to avoid another call to the server. I appreciate any help in clarifying my understanding and suggestion for workaround.
If you want to upload your SWF and have users access the configuration XML, you will need to host the XML somewhere reachable by your users. Your local machine (localhost:1111) is not reachable by anyone other than yourself (outside of some unusual hosts tweaking on the user's machine).
When you set up hosting and a web-server to actually serve the file over HTTP, you will need to do a few things:
Set up a crossdomain file on the server to define which hosts are allowed to load your configuration XML.
Amend your application to load data from the server, e.g. new URLRequest('http://your_domain_or_ip/config.xml').
The reason you cannot retain your reference to the XML file as a relative ./com/assets/config.xml is because the SWF will only load files over the local filesystem if it is being viewed as a file in the filesystem vs inside a browser.
When the SWF runs, the URLLoader instance you create will perform a HTTP GET to load your XML file.
If you want to avoid performing additional GET requests to fetch the XML, you will have to compile the configuration into the SWF itself using the [Embed] meta tag.

AS3: Download a download embedded file to desktop without browser

I'm creating an flash app where users can select something and download a temple. I'm publishing this file using air application with runtime embedded. In the app I've included a folder called documents with the individual files the user can download. Currently I'm using navigateToURL but I don't want it to rely on the browser. I've also tried this:
function surveyDownload(evt:MouseEvent):void {
var request = new URLRequest("document/template.docx");
var localRef = new FileReference();
try
{
// Prompt and download file
localRef.download( request );
}
catch (error:Error)
{
trace("Unable to download file.");
}
}
but all I get is the trace statement "Unable to download file".
How can I download an embedded file without the browser?
Your question is a little unclear. You want the user to download something from a server? If so then document/template.docx is not a URL so of course that will not work.
If you are talking about copying a file out of the AIR app bundle to the user's hard drive then you don't need URLRequest but rather the methods in the File class (browseForSave and copyTo).
Read the docs for File and search out some tutorials – there are loads and more complete than I would write here.

Access asset / resource file in an ActionScript Library project

I'm using an ActionScript Library project to share code and assets / resources between a Mobile and a Desktop ActionScript projects.
The library project has been added to the two other projects via the 'Add Project' option on the 'Library Path' tab, with the linkage type 'Merged into Code', and all the classes within it can be accessed by the other projects, and work properly.
However it contains a SQLite database file, which I want to copy out to the File.applicationStorageDirectory on the target system on the first load of the app, and I'm not sure how to get a reference to the file within the library project to copy it out.
The location of the db file is: LibraryProj - src/database/dbFile.db and I thought using File.applicationDirectory and then a path 'into' the swf would give me access to it, but none of the following tests say the file exists.
var test:File;
test = File.applicationDirectory.resolvePath("app:/src/database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("src/database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("database/dbFileDb.db");
trace("test.exists==" + test.exists);
test = File.applicationDirectory.resolvePath("dbFileDb.db");
trace("test.exists==" + test.exists);
Is this the correct method to copy resource / asset files out of a swf containing merged libraries and onto the app's storage directory? Is it even possible to share resources / assets from Library projects in this way?
Any advice would be very much appreciated.
After doing some more research about using the [Embed] tag in AS3, I've now worked out that this is what I should have been using to make the file available to consuming projects (I'd previously only used it for images, and didn't think of it for other file types too).
[Embed('igniteDb.db', mimeType="application/octet-stream")]
public static const myReferenceDbFile:Class;
To copy the file to the File.applicationStorageDirectory i'm using the following code. It converts the embedded file to a byte array, and then writes this out via a FileStream class to the destination file.
//write the embedded database file data into app user files directory
var bundleDbBytes:ByteArray;
bundleDbBytes = new myReferenceDbFile();//gets a reference to the embedded db file
var outputDbFile:File = File.applicationStorageDirectory.resolvePath(DB_FILE_NAME);
var fileStream:FileStream = new FileStream();
fileStream.open(outputDbFile, FileMode.UPDATE);
fileStream.writeBytes(bundleDbBytes);
fileStream.close();
And hey presto, the database is ready to use.