List the contents of a local folder from within swf - actionscript-3

I have succesfully read a file from the local computer in as3, I would like to know if it is possible to list the contents of a folder in some way. The swf will run from browser and acces local files only. No server, no outside world contact.
this is how I have accesed and displayed the local file.
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, useData);
loader.load(new URLRequest("abc.txt"));
function useData(event:Event):void {
var data:String = event.target.data.toString();
myTextField.text = data;
}

You can present the user with a standard OS window to open a file they can select themselves from their filesystem.
from the manual: FileReference.browse:
Displays a file-browsing dialog box that lets the user select a file to upload. The dialog box is native to the user's operating system. The user can select a file on the local computer or from other systems, for example, through a UNC path on Windows.
So even though you cannot list the files in a directory yourself, you can prompt the user to select a file for you.

Related

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.

How can I run a command in windows store app?

I need to run a command from windows store app?
the command is something like this : java -jar abc.jar
How can I do that?
EDIT :
I tried this but with no luck. It says file not found.
string exeFile = #"C:\DATA\start.bat";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
The app container blocks this behavior for Store apps.
First of all, you're attempting to obtain a StorageFile through your package InstalledLocation folder, which will not work. InstalledLocation is a StorageFolder, and its GetFileAsync looks for files only within that immediate folder. This is why it's returning file not found.
The API that takes an arbitrary path is Windows.Storage.StorageFolder.GetFileFromPathAsync. However, your ability to access files is limited by the app container. You can access files in your package folder or app data locations by default, or the various media libraries if you've declared access in the manifest, but otherwise you have to go through the file picker so the user is aware of what you're doing and can grant consent. Simply said, this is the only way you'll get to a file in a location like c:\data. You can play with this using Scenario 1 of the Association launching sample and the "Pick and Launch" button.
If you can get that access permission, then in you'll be able to launch a file if it's not a blocked file type. Data files (like a .docx) that are associated with another app work just fine, but executables are wholly blocked for what should be obvious security reasons. You can try it with the sample I linked to above--pick .bat, .cmd, .exe, .msi, etc. and you'll see that LaunchFileAsync fails.
Also note that the other launcher function, LaunchUriAsync, also blocks file:/// for the same reasons.

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.

Create text file in swf that downloads to users computer

Is it possible to create a text file on the fly in swf/flash running in a users’ browser and prompt the user to download the file to his computer?
I am familiar with the FileReference class, but I cannot figure out a way for this class to work without reading the file to be downloaded from a server.
Edit: For this to fully work in my use case the swf/browser should handle the file more or less like a file downloaded from remote servers - meaning that it should prompt the user and ask what he would like to do with the file and give the option to open file in default application (or open automatically if that is what the user has set as the default action for such files).
Try this:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")

c:\users\public folder not accessible from metro apps on x86 machines

I am not able to access C:\Users\Public\Documents folder from Windows store app running on a x86 machine using StorageFolder.GetFolderFromPathAsync("C:\Users\Public\Documents"). It works fine on my win8 desktop. any idea why I get this error?
Thanks,
MetroUI.
You can't access the file system outside your apps sandbox using that method. That will only access files in your apps isolated storage.
If you want to get files from outside the sandbox you will have to use a FileOpenPicker and have the user select the file
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
var file = await picker.PickSingleFileAsync();
if (file != null)
{
IRandomAccessStream stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
}
Then you would work with the data in the stream.
J.B is right, you can't access that folder directly due to sandbox limitations, but there are 2 other approaches you can take that might suite you better.
If you Add Documents Library to your app's Capabilities, you will be able to access the files in user's Documents library directly (it should include Public Documents as well, unless the user changed his settings), as long as you have declared a File Type Association for that extension to your app's Declarations. You can set both by editing Package.appxmanifest. Use the following code to access the StorageFolder:
var folder = KnownFolders.DocumentsLibrary;
Keep in mind that you need a company account to publish apps with Documents Library capability to Windows Store.
Alternatively you can use FolderPicker so that the user will grant you access to that folder. In this case you don't need any capabilities or declarations and you will be able to access all files:
var picker = new FolderPicker();
picker.FileTypeFilter.Add(".txt");
var folder = await picker.PickSingleFolderAsync();
You don't need the user to select the folder every time the app starts. You can store its reference to FutureAccessList to access it later and store the corresponding token (e.g. to settings):
var token = StorageApplicationPermissions.FutureAccessList.Add(folder);
When you want to access the folder again just use the token to get the folder reference:
folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(token);
var items = await folder.GetItemsAsync();