Home > Flex > Flex error #2176 when using FileReference

Flex error #2176 when using FileReference

The flash player 10 has new rules for UIA ( user initiated action ) so when I tried to use the FileReference save method I got the following error: Error: Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press. After I made some research I found a very cool article here, but I didn’t found any workaround.

Dispatching a MouseEvent won’t work, because the user must interact with the application by clicking a mouse button or by pressing a keyboard key.┬áI found a way to get rid of this error by displaying a popup and let the user to click a button in order to force the user to ┬áinteract with the application.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				layout="absolute" 
				minWidth="955" minHeight="600"
				creationComplete="creationCompleteHandler(event)">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
 
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				Alert.show("Now you can save the file!", "Test", Alert.OK|Alert.CANCEL, null, closeHandler);
			}
 
			protected function closeHandler( event:CloseEvent ):void
			{
				var fileReference :FileReference;
 
				if ( event.detail == Alert.OK )
				{
					fileReference = new FileReference();
					fileReference.save("http://www.bogdanmanate.com", "test.txt");
				}
			}
		]]>
	</mx:Script>
 
</mx:Application>
Categories: Flex Tags: ,
  1. Anonomyous
    March 22nd, 2011 at 22:04 | #1

    Great info. This was exactly what I was looking for.

  2. Irek
    August 31st, 2012 at 11:34 | #2

    Thx man! works perfect!

  1. No trackbacks yet.