write/save xml in as3 - actionscript-3

I want to create a stand alone pure flash application with external xml loading from the same dir. In that i want to update/overwirite my xml nodes and save it internally at run time (I dont want to go for fileReference class to save/overWrite my xml). when I load my apps next time it will react based on the updated xml.
I dont want go for AIR as it requires installation at end user side.
I cant use any server side script.
Is it possible in flash AS3.0?
Please suggest any possible solution.
Thanks..

it's possible with FileReference.save()
here is an example saving .txt files

var file:File = File.userDirectory.resolvePath("test.txt");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeUTFBytes("text to save");
fileStream.close();
last time I tried this worked without any prompt as to where to save . . . if it helps anyone.
(above worked with mobile devices saving to SDcard) - you will need to allow (WRITE_EXTERNAL_STORAGE) permission.

You can't access files in Flash without AIR and FileReference, but you can save data in SharedObject. This is analogous to cookies in browser (but not tied to one). Data will be saved somewhere in "C:\Users\UserName\Application Data\Macromedia\Flash Player\" directory (example for Win7) until user clears it or available disk size is exceeded.

Related

AS3 Flash - Save JPG

Im making a little game with in the end the option to take a picture of you to share. Ive succeeded in coding parts like displaying the foto on a bitmap and to save the foto.
Now the problem is that you get a save-as pop-up screen to select where you want to save your foto. But i dont want that option, i just want to save it at a specific folder on my pc. Ive come across similar questions here, to save png/jpg foto's, but none have my specific question.
Is is possible to do?
Thanks,
The.Jack
You can't save directly without a dialogue-box in web, it always asks where it should save. Because it should triggered by user due to flash security issues
But in Adobe Air it is possible to save with flash.filesystem.File and flash.filesystem.FileStream
var fileStream : FileStream = new FileStream();
var file : File = File.desktopDirectory.resolvePath('sample.jpg');
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(defined_byte_array);
fileStream.close();
Hope it helps

Download multiple file without user interaction in flex web application possible?

Before asking question I says that read properly what is question.
I am creating web application for download videos files.
In which I am going to create functionality like. User select multiple file to download. The Video URL come from webservice.
Question:
Now, I need that when user click on Downloads then open new page which is in flex. Now, suppose there is 5 files to download, then it will start automatically to download first file. which shows progress of downloading.
Once Downloading Done of first file then start for Second file, then third and So on.
The files are download in default path of browser OR in specified of flex code. And file size may large. (More than 500 MB).
I search lot for it but, didn't get any proper solution. It says that it's not possible without user interaction.
But, If user interaction is required then user has to continuously watch to download that large file and then need action for download second file.
Is it possible using flex? If yes then How?
Any help will appreciate.
Thanks,
As you've stated the question it is not possible to do this.
From the web Flash Player you only have 2 options:
Use FileReference/save() which requires the user to choose where to save the file.
Use navigateToURL() to point the browser to the file location, which lets the browser decide how to handle the download -- immediately download it to the default download directory, or let the user choose. This isn't as reliable. (A variation of this is to use ExternalInterface to call JS window.open, which is the JS equivalent to navigateToURL.)
In either case you cannot choose where to download the file to from Flex, or even know where they do download it, and you can't use either of these options without user interaction (such as clicking a button).
Using AIR (desktop application) you can do this using File and FileStream.
Create a method to load your file (give a file path as parameters or use an Array or Vector with all respective urls). As soon as you finish the first download, check if there is more items in your array, and if so, load the subsequent index. It's not necessary that the user will need to have any interaction.
Example of loader with init, progress and complete states.
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
myLoader.load(new URLRequest("yourFile.png"));
function initListener(event:Event):void
{
// just started the loader process
trace("Object Initialized");
}
function progressListener (event:ProgressEvent):void
{
trace("Downloaded " + event.bytesLoaded + " out of " + event.bytesTotal + " bytes");
}
function completeListener(event:Event):void
{
// here you can load the next file
trace("Loading Completed");
}
For Flash player security reasons It's impossible to download a file in background mode without user interaction. I worked on something like what you are doing for a web tv project, where user can download a video and it's attached files (some hundreds), for that I used a server side PHP script which is called using navigateToURL() to download the video file and a zip which contain all attached files (the PHP script is called twice).
So, for that you can do :
PHP :
<?php
// some controls here
$file = $_GET['f'];
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: '.filesize($file));
readfile($file);
exit();
}
?>
ActionScript :
const download_url:String = 'http://www.example.com/download.php?f=';
var files:Array = ['01.mp4', '02.mp4', '03.mp4', '04.mp4', '05.mp4'];
var index:int = 0;
var timer:Timer = new Timer(100, files.length);
timer.addEventListener(
TimerEvent.TIMER,
function(e:TimerEvent){
navigateToURL(new URLRequest(download_url + files[index]));
index ++;
}
)
timer.start();
Of course this code will download files via the browser as any file downloaded by user. Note here, that sometimes browser can block download because it's considered as a popup, so you have to mention that to your users to give your site permission to download files.
This code is just an example to show you a manner how to do what you want, may be you are not using PHP in your server, so you have to improve and adapt it to your specific needs.
Hope that can help.

Can't find image exported from AIR app on Android phone

I feel as if I'm missing something very basic here. I'm trying to add an option to let export images from an Android app in PNG format. The code seems to work fine on my Droid Bionic and doesn't error out, but afterwards I can't find the image anywhere in the phone's storage. Where is it going? Is it possible to access images exported from an AIR app? Am I missing something completely obvious?
bdToSave.draw(screenshot);
var arrBytes:ByteArray = PNGEncoder.encode(bdToSave);
var file:File = File.desktopDirectory.resolvePath("testimage.png");
var fileStream:FileStream = new FileStream();
try {
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(arrBytes);
fileStream.close();
}
catch (errObject:Error) {
Env.outBox.text = "Error writing image file!"
}
I've also used the CameraRoll class to successfully export images and found the resulting files easily, but the images there seemed to use bad JPEG compression. So I was hoping using FileStream instead would result in nicer images.
File.desktopDirectory does not exist on Android. Use File.applicationDirectory or File.applicationStorageDirectory to write to the internal disk. You almost always want to use applicationStorageDirectory and I believe that is actually the only one you can use on iOS for writing data. For writing to external drives, and this is untested on my part, I believe you want to use File.documentsDirectory. Trace out File.documentsDirectory.nativePath to verify that is the correct path.
It might be wise to consider that the majority of higher-end phones being released now do not contain SD cards. If this is meant to be a mass market product, I'd recommend using applicationStorageDirectory instead.

Creating an external .txt in as3

how do I automatically create an external .txt file without asking the user for directory. I am using the following code so as to create and save the ".txt "file, but my code asks for the directory to save the file. I want the file to be saved automatically without asking user for the directory...
import flash.net.FileReference;
import flash.events.Event;
addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
function saveFile(event:Event):void
{
fileRef=new FileReference();
fileRef.save(ss,"save.txt");
removeEventListener(Event.ENTER_FRAME, saveFile);
}
Are you creating an AIR appplication for desktop? If so, you can use File and FileStream.
var f:File=new File("path\to\file.txt");
var str:FileStream=new FileStream();
str.open(f, FileMode.WRITE);
str.writeUTFBytes(contents);
str.close();
If you are creating a flash-player application, then you must ask the user for a path to save (because an online app saving without a user's knowledge is just WRONG)
You cannot do that for security reasons if it's a browser app... Maybe you can try using SharedObjects instead...
is definitely a security issue - saving files without the user knowing what you're doing just isn't right. AIR is the exception - as a "normal" appliaction it can save a file without asking where to put it.
If you are running in a browser, you CAN save to a file, you have to use external interface and be on file:/// protocol and use something like jQuery.twFile.js
Can be done if you compile to AIR (Desktop app). But cannot be done with browser. As i think, it will go against browser's security. Just think, if it were possible, then websites would start storing and loading content on your system without your permission. :)

How can I make Air remember the location of last opened file?

All the walkthroughs I've seen specify either desktop, local files as the initial view when calling browseForOpen.
Is there a way to have the air application remember the last location a file was loaded from and default back there?
If necessary I suppose the path could be written to the local db, but even still, I do not know how to get the filepath of the loaded file.
You can use File.nativePath and File.resolvePath.
A few years later, perhaps, but thought this might help someone in the future.
var file:File = new File();
file.browseForOpen("Open");
This will open the last location.