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:38

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.

查看更多
Juvenile、少年°
3楼-- · 2019-04-07 01:41

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
查看更多
登录 后发表回答