1

I have created a painting app and when the user want's to save his drawing he simply hits the save button and a dialog pops up - or should do.

When testing locally on dev machine it's no problem but whenever the app is loaded in a browser it won't work. Even if I load the local SWF file in a browser it won't work.

The following code is what I use to save the file and as said it works fine locally but whenever it goes into a browser it dosen't.The save dialog simply never pops up.

Any suggestions?

var bitmapData:BitmapData = new BitmapData(canvas.width, canvas.height);
bitmapData.draw(canvas);
var jpg:JPEGEncoder = new JPEGEncoder(100);
var ba:ByteArray = jpg.encode(bitmapData);

file = new FileReference();
file.addEventListener(Event.COMPLETE, fileSaveComplete);
file.addEventListener(IOErrorEvent.IO_ERROR, error);
file.save(ba, 'FileName.jpg');

Running FlashPlayer 11 and compiling to 10.2 BTW.

Martin
  • 548
  • 5
  • 14

1 Answers1

5

That is a security issue. A save file dialog window can now only be opened as a result to a direct user event, like a click. One solution is to notify the user when the image is created, and ask if he wants to save the file locally. And if he clicks yes, you call the save method.

Here's more info on this change:

http://www.bogdanmanate.com/2010/05/12/flex-error-2176-when-using-filereference/

Hope this helps. Have a good day.

Romi Halasz
  • 1,949
  • 1
  • 13
  • 23
  • 2
    Thanks a lot. The code I pasted above is called from a Custom Event I created and that's why it's not working. I changed it so it's called from a MouseEvent a there we go! Good day to you too. – Martin Feb 15 '12 at 13:10
  • Great! It might be an annoying feature, but it really prevents some nasty stuff. – Romi Halasz Feb 15 '12 at 13:15
  • On a side note, doesn't it throw and error in the debug player if you try to call FileReference.save() without a UIA? – joncys Feb 15 '12 at 13:38
  • I didn't try a try & catch statement but at least it did not throw the IOErrorEvent which I was listening for. If just it threw that I would have had something to aim for... – Martin Feb 15 '12 at 14:14