I need to call FileReference.save() after a web service call has completed, but this method has a restriction: \"In Flash Player, you can only call this method successfully
As a workaround I used the ExternalInterface class. I created a javascript function with this code
function downloadFile (url) {
window.open(url);
}
An in AS3 I call
var url = 'www.example.com/downloadfile.php?file_id=xxx';
ExternalInterface.call('downloadAttachmentFile', url);
So with that I transfer the file handling to JS/HTML.
I had this same issue, I chose to use flash.net methods. Call flash.net.navigateToURL(url);
from an actionscript or navigateToURL(url);
from mxml.
This is a comment on Thomas' answer (I don't have enough XP to comment yet): The mousedown
and mouseup
workaround works nicely. Just a note that if you make any changes in prepare_PDF()
that need 'undoing' in save_PDF()
, then its a good idea to call that code on the mouseout
event as well, since there might be a case that the user mousedown's on the button, but then moves the mouse away from the button.
This was particularly relevant for my case, in which we increase the size of a watermark on an image when the user clicks the download button (that triggers the .save()
call). I reduce the size of the watermark down to normal on the mousedown
and mouseout
events.
Adobe does this as a sort of security measure to ensure users are the ones messing with files rather than potentially harmful code. My understanding is that they enforce this by only allowing handlers of (click?) events that originate from UI components to execute the FileReference methods, so generating your own events programmatically will not work, although I have not tried to verify this. Unfortunately the best resolution I've found is to re-work the UI a bit to conform to this constraint. In your particular situation, you could make this a two click process with a button that says something like "Prepare Download", which changes to "Download File" after the web service is complete. This is less than ideal from a user perspective, but I don't think there's much else that can be done unless you can somehow complete your web service call prior to displaying the button that triggers the FileReference.save() call.
What i do to solve this is to show an alert message with an anonymous function so i don't have to create a button.
Alert.show("Do you wish to download the file?", "Confirm", Alert.OK | Alert.CANCEL, this, function (eventObj:CloseEvent):void {
if (eventObj.detail == Alert.OK) {
fileReference.save(zipOut.byteArray, dateFormater_titulo.format(new Date ()) + ".zip");
}//if
}/*function*/, null, Alert.OK);
After struggling for that for well, a couple hours I found a workaround: you can use both mouseDown AND mouseUp events instead of just click.
For instance: s:Button mouseDown="prepare_PDF()" mouseUp="save_PDF()"
Works fine for me!
Happy coding!
--Thomas