FileReference.save() AS3 上锁定文件类型的替代方案

发布于 2024-11-18 02:07:55 字数 1075 浏览 2 评论 0 原文

更新:

正如下面 Jacob 的回复中所讨论的,限制或纠正 FileReference.save 的行为是不可能的。谁能建议一个替代方案(服务器是 Apache/PHP),它符合我在这篇文章中的所有标准,并避免我与 Jacob 讨论的陷阱?

结束编辑

我正在使用 FileReference.save() 从 AS3 应用程序保存图像。这是代码,运行良好:

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");

这将按预期打开保存文件对话框。我使用它而不是将 byteData 发送到 PHP,因为我希望用户有一个熟悉的对话框,让他们设置自己的文件名。

当用户像我一样将操作系统配置为显示文件扩展名时,就会出现问题。这意味着在保存对话框中,文件名包含如下图所示的扩展名,因此用户在重命名文件时很容易删除该扩展名。由于此框的默认文件类型是“所有文件”,因此如果删除扩展名,则文件将以无类型保存。

我不知道有什么方法可以在保存对话框上强制使用文件类型(如果有一个是我的首选路线),因此如果失败,任何人都可以建议我采取一种安全的方法来执行此操作,但仍允许用户设置文件名本身使用操作系统的标准对话框?

我确实尝试在 save() 之前调用 FileReference.browse(),如 本教程,但这会引发错误,因为您一次只能执行一个 FileReference 调用。

文件保存对话框

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

轻拂→两袖风尘 2024-11-25 02:07:55

抱歉,这是一个已知的主要 Flash 播放器错误,没有真正的解决方法。

在这里投票:https: //bugs.adobe.com/jira/browse/FP-2014

(尽管出于某种原因,Adobe 将其称为功能请求。)


编辑2:

针对 Chrome 的 FP 11.8 进行了修复,适用于 Mac OS 和 Windows。

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


编辑:添加navigateTo解决方法信息

有执行navigateTo()的php选项:将文件发布到如果您指定正确的标头,您可以让浏览器打开浏览器的本机保存对话框,该对话框确实维护扩展。然而,该方法还有其他缺点:

  • Flex 内部没有错误处理、
  • 额外的请求时间、
  • 额外的服务器负载。
  • 上传和重新下载文件时不会向用户提供任何反馈。
  • 跨浏览器/操作系统的用户体验不一致。
    • 某些浏览器实际上会弹出一个新窗口,然后消失
    • 有些人会离开窗口。
    • 有些根本不会有任何新窗口。

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.
情绪 2024-11-25 02:07:55

经过长时间的讨论并考虑到我们的用户群,我们的解决方案是:

  1. 检测问题何时发生:在 FileReference SELECT 事件侦听器中,检查用户是否更改了文件名并且扩展名已被删除。
  2. 如果出现问题,请取消操作。使用“服务器反弹”方法和navigateToURL()再次启动操作

我们发现大多数时候用户没有更改名称,因此我们很少需要依靠第二种方法,这是不太理想的。

请注意,为了缓解前面提到的 navigatorURL() 浏览器不一致问题,我们发现在服务器返回的标头中指定“Content-disposition:attachment”很有用。

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.

羁拥 2024-11-25 02:07:55

可惜这个不可用。我可以理解用户可以自由地按照自己想要的方式保存文件。但是如果您忘记添加扩展程序怎么办?根据我的经验,Flex 然后将文件保存为不带扩展名的普通文件。通过这样做,一些用户可能不知道如何处理他们保存的文件。因为并非每个用户都会知道不同扩展名的含义,并希望打开具有错误扩展名的文件。我经常看到人们悄悄地询问希望打开一个具有奇怪扩展名或根本没有扩展名的特定文件。

我部分理解为什么 Adob​​e 将其视为功能请求而不是错误。但看到它如何扰乱事情的进程,你可能宁愿将其视为一个错误。正如我所说,并非每个用户都知道如何打开名为“myfile”而不是“myfile.jpg”的文件。有多少了解如何打开它的用户将“.cow”设置为图像查看器打开的扩展名,操作系统将如何响应这种奇怪的扩展名?好吧,也许“.cow”是一个坏例子,但我想你明白了。

解决此问题的方法可能是您自己创建该函数。这意味着您向用户提供一个弹出窗口(或您喜欢的方式),其中包含指定文件名、获取文件位置和保存/取消按钮的选项。然后,您可以检查他们是否添加了扩展名,填写了文件名并给出了文件位置(甚至是正确的)。这与 FileStream 函数相结合应该(如果我没有记错的话)是创建与保存对话框选项类似的函数的解决方案。

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.

佼人 2024-11-25 02:07:55

Eduard 在 Adob​​e 的功能请求跟踪器中针对此问题提供了解决方法:将文件加倍扩展名,例如“image.jpg.jpg”。这就是我们要做的,因为对于隐藏扩展名的用户,无论他们是否更改文件名,扩展名都会被删除。

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.

第七度阳光i 2024-11-25 02:07:55

答案是……你不知道。它不仅不是 FileReference(或 File,即 Adob​​e Air)的一部分,而且也不是我们所关心的。如果用户想要将所有文件存储为 .cow,那就让他吧!也许他已经配置了他的机器,以便 .cow 将作为 jpeg 打开。

老实说,作为一名用户,当有人认为他们有权告诉我要在我的机器上命名文件时,我真的很困扰。

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.

無處可尋 2024-11-25 02:07:55

我知道这篇文章发布已经有一段时间了,但是......参考 Amoeba.co -tutorial,你可以这样做(示例适用于 PNG 图像):

files = new FileReference();
files.browse(new Array(new FileFilter("Image (*.png)", "*.png")));
files.cancel(); // Important
files.save(/* Your Data Here */);
  • 中性

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
只有一腔孤勇 2024-11-25 02:07:55

此解决方案没有真正的解决方法,因为它与 Windows 中的默认设置有关。微软认为人们不需要知道扩展名,因此创建了一个隐藏已知文件类型的文件扩展名类型的设置。要解决此问题,用户需要按照此处显示的步骤清理此设置

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

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

小梨窩很甜 2024-11-25 02:07:55
file.save(byteData, "myImage.jpg"+Math.floor((Math.random())*100)+".jpg");

你的问题已经解决了。

file.save(byteData, "myImage.jpg"+Math.floor((Math.random())*100)+".jpg");

Your problem is solved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文