Alternative to locking file type on FileReference.save() AS3

shanethehat picture shanethehat · Jun 30, 2011 · Viewed 9.7k times · Source

Update:

As discussed in Jacob's reply below, limiting or correcting the behaviour of FileReference.save isn't possible. Can anyone suggest an alternative (server is Apache/PHP) that matches all of my criteria from this post and avoids the pitfalls I discussed with Jacob?

End edit

I'm saving an image from my AS3 app using FileReference.save(). This is the code, which works fine:

var encoder:JPGEncoder = new JPGEncoder(80);
var byteData = encoder.encode(myBMD);  //bitmap data object created earlier
var file:FileReference = new FileReference();
file.save(byteData, "myImage.jpg");

This opens up the save file dialog as expected. I'm using this rather than sending the byteData to PHP because I want the user to have a familiar dialog box that lets them set their own file name.

The problem comes when users have their operating system configured to display file extensions, like I do. This means that in the save dialog the file name contains the extension as seen in the image below, and so it is very easy for the user to delete that extension when they rename the file. Because the default file type for this box is 'All files', if the extension is deleted the file is saved with no type.

I don't know of any way to force a file type on the save dialog (if there is one that would be my preferred route) so failing that can anyone suggest a safe way for me to do this that still allows the user to set the file name themselves using a standard dialog for their OS?

I did try putting in a call to FileReference.browse() before the save() as shown in this tutorial, but that throws an error because you can only perform one FileReference call at a time.

file save dialog

Answer

Jacob Eggers picture Jacob Eggers · Jun 30, 2011

Sorry this is a known, major flash player bug with no real workaround.

Vote for it here: https://bugs.adobe.com/jira/browse/FP-2014

(Though for some reason, Adobe is calling it a feature request.)


Edit 2:

Fixed for FP 11.8 for Chrome, on both Mac OS and Windows.

https://bugbase.adobe.com/index.cfm?event=bug&id=2898055


Edit: Adding navigateTo workaround information

There is the php option of doing a navigateTo(): posting the file to a php page which has a mime-type of image/jpeg If you specify the right headers, you can get the browser to bring up the browser's native save dialog which does maintain extensions. However, there are other drawbacks to that method:

  • no error handling from within flex
  • additional request time
  • additional server load.
  • no feedback to user while the file is being uploaded and re-downloaded.
  • inconsistent user experience across browser/os.
    • some browsers will actually popup a new window that disappears
    • some will leave the window.
    • some won't have any new window at all.