Create text file in swf that downloads to users computer - actionscript-3

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")

Related

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.

how to get browsed file location by user when he wants to download a file

this is my html code to make user to download a file and it is hitting controller
window.location.href="#routes.ListManagementController.downloadList("+listName+")?listname="+listName;
this is my controller code:
String listName = Form.form().bindFromRequest().get("listname");
response().setContentType("application/x-download");
response().setHeader("Content-disposition", "attachment;filename="+listName+"_data_export.csv");
The above two respose() statements make a pop up to download a file I want the browsed file location
File file = new File("C:/csv/" + filename);
So, using servlet api we can write the content into browsed file location using respose.getOutputStream() method. In play there are is no support for servlet. I want browsed file location selected by user so that i can give that location to File and write the file over there.
You can't get the location of a directory on the client, and even if you could, your server side could wouldn't be able to write to it (since it would usually be on a different computer).

Downloading and replacing file in package

In my Visual Studio 2013 Solution, I added a folder "mAppData" with some static Content for the app.
One of the files is a Textfile ("imprint.txt").
On a phone page, I Display the content of this file. This works fine.
Is there a way to replace this file on runtime? I want to download new Content from web and replace this file with the downloaded content.
rather than downloading and replacing the "imprint.txt" you could simply download the imprint as text (from your imprint.txt - stored online) and display it:
var client = new System.Net.WebClient();
client.DownloadStringCompleted += (sender, args) => MessageBox.Show("downloaded imporint: " + args.Result);
client.DownloadStringAsync(new Uri("http://yourSite.ch/imprint.txt"));
My sample simply shows a MessageBox with the downloaded text. However, you can update a text control or similar.
If you want the content to be available even if you are temporary offline, you can store the file in the filestorage.
Consider adding multiple files for different languages in your build and load the file depending on the localization of your phone.
However - replacing a existing file for your Solution is not allowed on runtime. Consider this as a safety feature. If the Codebase of your app could change during runtime, your code would be injectable by an attacker.
You can't alter the package content at runtime, it is read-only.
The place where you can store data locally is the Isolated Storage. What you should do is create a file in the Isolated Storage at first launch that stores the original content of your file ("imprint.txt") and then you'll be able to change it any time you want.
Here is a tutorial that explains how to read and write text in the Isolated Storage: All about WP7 Isolated Storage - Read and Save Text files

Load local image, modify it and save back to same file

File input allows user to access a local file from browser. Is it possible to load a local file given by file input, modify it and save it back to same local file? I know that HTML5 allows creating writeable filesystem, but basically it seems to be abstract directory.
For security reasons, I don't think the browser can overwrite the local file. Using the File-System API you could only copy the contents of the local file to the sandboxed File-System API directory(found under various obfuscated file names). All manipulation/saving would be done in AppData.
Perhaps displaying the modified image on the screen, right click, save-as to the given file location would also be suitable? (other than that I think you have to upload the image to a server and download it back again)

FileReference vs File?

I want to make a Flash app in which the user can load and save files from and to their local hard drive. While I've often done this in AIR apps using the File and FileStream classes, I haven't done so before in an SWF.
From what I know the FileReference class is used for this, although it seems to have some restrictions due to security risks. I'd like to know what the main differences are between using the FileReference class and using the File and FileStream classes to load and save files.
The File class extends the FileReference.
FileReference is safe to be used in the FlashPlayer (in the browser) because it won't let you modify the files in the user machine.
If you want to open a file, you need to as for the user to open it for you with: FileReference.browse().
To save a file, you need to ask the user to save it: FileReference.save()
With File class you can open, modify and save files without those dialogs.
Furthermore, the File class gives you a bunch of useful properties like: File.desktopDirectory, File.documentsDirectory and such.
You can check if a file exists with the exists property and have a much restriction when manipulating file in the user file system.
You can read more about the FileReference and File classes in the docs.