Skip to main content
Inspiring
February 21, 2011
Question

Downloading a file

  • February 21, 2011
  • 2 replies
  • 502 views

Hi Folks,

Can any one explain the following to me:

Used the following code in a real life demo. I clicked the button, SaveAs dialog displayed, I chose to say the file in the Documents folder.

Went to open the file, and got a message saying it was in use with another process. Restarted my computer, was now able to open the file. However the file is empty. It should have some text that I put in there - how can text vanish from a file? By simple downloading it.

info_form.download_file.buttonMode = true;

info_form.download_file.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(event:MouseEvent):void {
var pdf = new FileReference();
pdf.download(new URLRequest(" http://www.somesite.co.uk/media/myFile.txt"));
}

Am I missing something here?

Any help appreciated.

Kind Regards,

Boxing Boom

This topic has been closed for replies.

2 replies

Inspiring
February 22, 2011

To Use FileReference you will have to publish to AIR

(don`t assume any user will install AIR only to download a text file).

The default behaviour of any browser I know of is to download a file it can`t display.

So If you would simply zip that text file on the server, you wouldn`t need FileReference and AIR at all.

mkayyali
Inspiring
February 22, 2011

Try this one:

import flash.events.*; import flash.net.FileReference; import flash.net.URLRequest; import flash.net.FileFilter; info_form.download_file.buttonMode = true; info_form.download_file.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); var pdf:FileReference; function mouseDownHandler(event:MouseEvent) {      pdf = new FileReference();      configureListeners(pdf);      pdf.download(new URLRequest("http://www.somesite.co.uk/media/myFile.txt"), "myFile.txt"); } function configureListeners(dispatcher:IEventDispatcher):void {      dispatcher.addEventListener(Event.COMPLETE, completeHandler);      dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);      dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); } function completeHandler(event:Event):void {      trace("completeHandler: " + event); } function ioErrorHandler(event:IOErrorEvent):void {      trace("ioErrorHandler: " + event); } function securityErrorHandler(event:SecurityErrorEvent):void {      trace("securityErrorHandler: " + event); }

if you got any errors please reply...

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/FileReference.html#download()