Alternative to locking file type on FileReference.

2019-04-07 00:39发布

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

8条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-07 01:25

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

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-07 01:31

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.

查看更多
The star\"
4楼-- · 2019-04-07 01:33

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.
查看更多
ら.Afraid
5楼-- · 2019-04-07 01:33

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.

查看更多
贪生不怕死
6楼-- · 2019-04-07 01:35

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.

查看更多
我只想做你的唯一
7楼-- · 2019-04-07 01:35
file.save(byteData, "myImage.jpg"+Math.floor((Math.random())*100)+".jpg");

Your problem is solved.

查看更多
登录 后发表回答