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

8

8

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

Upheave answered 30/6, 2011 at 17:54 Comment(0)
V
4

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.
Vardon answered 30/6, 2011 at 17:59 Comment(5)
That's not actually quite the same. That bug relates to the download() method when the user is hiding their extensions. I haven't tested if that will also affect me yet (although I suspect it will). Voted it anyway.Upheave
I hadn't even thought of this because I don't hide my extensions. Maybe a PHP solution is a better way to go, although I don't then know how to give the users the ability to rename the file as they download it.Upheave
Yeah, I just reread your issue. It is different, but related in that allowing the developer to specify a file type would prevent both problems. Also, I can attest to the fact that FP-2014 also affects the save() method.Vardon
Last time I did this I ended up sending the data to PHP, but that is unpleasant because of the differences in how browsers handle that. Some (looking at FF here) do not allow you to alter the file name or download location, and others (IE especially) open a blank tab for the download that steals the focus and does not close automatically.Upheave
@shane I've gone through the same discussions of tradeoffs before as well. We decided (not unanimously) to live with the save() issues and train our smallish userbase.Vardon
B
2

After long discussions and given our user base, our solution is:

  1. detect when the problem occurs: in the FileReference SELECT event listener, check if the user has changed the file name and the extension has been dropped.
  2. if the problem occured, cancel the operation. Start the operation again using the 'server bounce' method with navigateToURL()

We found that most of the time, the user did not change the name, so we rarely need to fall back on the second method which is less desirable.

Note that to mitigate the navigateURL() browser inconsistency problems mentioned earlier, we found it useful to specify "Content-disposition:attachment" in the header returned by the server.

Belgium answered 24/1, 2012 at 9:38 Comment(0)
P
1

Pity that this isn't available. I can understand that users what to have the freedom of saving the files the way they want. But what if you forget to add the extension? In my experience Flex then saves the file as a normal file without an extension. By doing that some users might not know what to do with the file they saved. As not every user will know what the different extensions mean/are and hope to open files with wrong extensions. I've seen it quiet often that people ask hope to open a specific file with a strange extension or no extension at all.

I partially get it why Adobe sees it as a feature request rather then a bug. But seeing how it can mess up the process of things you maybe rather want to consider it as a bug. As like I said, not every user will know how to open the file named "myfile" instead of "myfile.jpg". And how many users that do understand how to open it have set ".cow" as an extension to be opened by the imageviewer, and how will the OS respond to such strange extension? Well maybe ".cow" is a bad example, but I guess you get the point.

A work-around for this could be to create the function yourself. Meaning you give the user a pop-up window (or how you prefer it) with the option to specify the filename, get the file location and then save/cancel button. You then can check if they added an extension, filled in a filename and gave a file location (that is even correct). This combined with the FileStream function should (if I'm not mistaken) be a solution to create a similar function as the save dialog option.

Palmetto answered 11/8, 2011 at 10:47 Comment(0)
A
1

Eduard provided a workaround in Adobe's feature request tracker for this issue: Doubling the file extension, such as "image.jpg.jpg". This is what we will do since for our users who hide extensions, the extensions gets dropped whether or not they change the file name.

Alewife answered 9/7, 2012 at 21:39 Comment(0)
N
0

The answer is... you don't. Not only is that not a part of FileReference (or File, which is Adobe Air), it also is not our concern. If a user wants to store all of the files as .cow, let him! Maybe he has configured his machine so that .cow will be opened as a jpeg.

Honestly, as a user, it really bothers me when someone believes that they have the right to tell me what I am going to name files on my machine.

Nimble answered 30/6, 2011 at 18:0 Comment(2)
It wasn't part of the question, but the real problem is when the user hides extensions and then renames the file. The file actually loses it's extension and Windows no longer knows what kind of file has been downloaded rendering the user unable to open the file. OSX users don't have this problemVardon
I absolutely agree that for power users this is frustrating, but my concern here is catering for my target audience, who are not necessarily as savvy as you or I and could delete the extension by accident and not know how to fix it. This app is targeted mainly at 8-30 year old females who probably are not from an IT background.Upheave
F
0

I know it's been a while since this was posted, but... Referring to the Amoeba.co -tutorial, you can do this (the example is for PNG-images):

files = new FileReference();
files.browse(new Array(new FileFilter("Image (*.png)", "*.png")));
files.cancel(); // Important
files.save(/* Your Data Here */);
  • Neutral
Flowerdeluce answered 27/9, 2011 at 14:1 Comment(1)
doesn't work. the files.cancel() does not close the browse window automatically like you might expect.Apache
L
0

There is no real workround for this solution as it is related to a default setting in windows. Microsoft decided that people do not need to know the extension names and thus created a setting that hides file extension types of known file types. To fix this the user need to clean this setting by following the steps shown here

http://windows.microsoft.com/en-in/windows/show-hide-file-name-extensions#show-hide-file-name-extensions=windows-7

Livengood answered 26/8, 2013 at 6:2 Comment(0)
K
-1
file.save(byteData, "myImage.jpg"+Math.floor((Math.random())*100)+".jpg");

Your problem is solved.

Kristeenkristel answered 29/12, 2015 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.