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

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.

Related

Playing local HTML5 audio from an external server on desktop

My problem corresponds with the following hypothetical situation:
I have a website with a blog, which stores a playlist of music (just the filenames). I can edit this playlist remotely, for example from my phone when I am on the move. The content of the website is stored in a database on the server (MySQL), which cannot be accessed remotely.
When I get home and I am writing on my blog, I want to play the files in the list, on the same website, by using HTML5 audio. The files are located on my local computer at home. Hence I want to access these local files through my website.
An example of how I am addressing a local file is file:///M:/music.mp3.
The whole set-up works if I work from localhost, so I don't think it is a coding issue.
The problem is that both Firefox and Chrome, my favourite browsers, do not allow third party websites to access my local files without my active input.
It makes sense that the construction above is prevented by user agents due to security issues. I was hoping to find a solution in the fact that I use browser integrated HTML5 audio; IMO, there would be no security issue since the files that are loaded by HTML5 audio cannot be accessed via DOM, so some proper coding of the browsers would have left some breathing room here.
Some extra conditions:
I am looking for the blog and the music player to remain integrated.
I don't want to store my music files remotely.
I don't want to set up a local server.
Perhaps there is a way to add a security exception for my website to my browser, but this seems no longer the case for Firefox.
Any suggestion is most welcome!
I would suggest taking a look at the File System API. It's a very new API that's currently only working with chrome unfortunately :/
What you can basically do is request for access to the local filesystem using window.requestFileSystem() or window.webkitRequestFileSystem(), which has a success callback with a FileSystem object. Using this objects root (a Directory Entry object) you can look up the file using root.getFile(). This has a callback with a FileEntry object. You can then use this file entry to get the actual File object using fileEntry.file() and pass that into a FileReader object to get the data url. Untested example below, probably not perfect and you probably have to tweak with webkit prefixes.
// when file system loads
function onFs(fs){
// locate file
fs.root.getFile('M:/music.mp3', {/*options*/}, function(fileEntry){
// get file / blob
fileEntry.file(function(file) {
// read file
var reader = new FileReader();
reader.onload = function(event) {
var dataUrl = event.target.result;
// create new audio object with data url
// e.g. <audio src="dataUrl" />
};
reader.readAsDataURL(file);
// handle error
} , onError);
// handle error
}, onError);
}
// Opening a file system with temporary storage
window.requestFileSystem(TEMPORARY, 1024*1024 /*1MB*/, onFS, onError);
(I'd like to also mention, I've never used the File System API, but it seems promising)

How save a snapshot of a chart in flex?

This is the method i'm using to save a snapshot of a chart in flex.
private function takeSnapshot():void{
var image:ImageSnapshot = ImageSnapshot.captureImage(chart);
var file:FileReference = new FileReference();
var fileName:String = "chart.png";
file.save(image.data,fileName);
}
this method always ask saving path(give prompt) even if i give file name with path. how can i save snapshot without having a prompt ?
I should have asked that in a comment (whether it's a web app or Air). But it seems i need some more reputation to do that :(.
So here is the deal , if your application is a flex web project, you can't save the screenshot file without having the save popup appear. But if it's an AIR application you can save it in a default location.
For web app, you cannot save files directly to local file system. it's a browser security issue. If the snapshot is not going to be opened outside the app, the way to save it without prompt is to save it as shared object.

Can you locally create a HTML form that outputs a .txt file to be saved?

Can you create a html form that can be housed on a USB flash drive and opened up in a browser that allows someone to enter info and then allows them to save what they entered as a .txt file back to the same USB? Any ideas or resources you can point me to?
Not a full solution, but maybe it gets you on the right track:
Generate a usual HTML page with a form to enter all information necessary.
Then use JavaScript to build a string containing all data you want to store inside the textfile.
Create a Blob() object out of it (MDN docu) - the type application\octet-stream is important to force a download later on:
var myBlob = new Blob( content, { "type" : "application\/octet-stream" });
Convert that blob to a DataURL using window.URL.createObjectURL (MDN docu):
var dataUrl = window.URL.createObjectURL( myBlob );
Update the location of your browser tab using window.location and set it to your data url:
window.location = dataUrl;
The user will then have the usual "Save file as ..." dialog for your generated textfile. Note, however, that this way you are not able to set the name of the textfile!
Not directly. Since this type of form processing has to occur server-side, you need a web server.
Now, it is entirely possible to run Apache or something similar from that flash drive, and have PHP or something do your file writing. This isn't as straightforward as you are looking for, but is possible. Keep in mind that not everyone has autorun enabled, that folks use different OSes, and that firewalls are often picky about new applications opening up ports.

write/save xml in as3

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.

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. :)