在 FileReference.save() AS3 上锁定文件类型的替代方法 [英] Alternative to locking file type on FileReference.save() AS3

查看:42
本文介绍了在 FileReference.save() AS3 上锁定文件类型的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

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

结束编辑

我正在使用 FileReference.save() 从我的 AS3 应用程序中保存图像.这是代码,可以正常工作:

var 编码器:JPGEncoder = new JPGEncoder(80);var byteData = encoder.encode(myBMD);//之前创建的位图数据对象var file:FileReference = new FileReference();file.save(byteData, "myImage.jpg");

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

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

我不知道有什么方法可以强制保存对话框中的文件类型(如果有一个是我首选的路径),所以如果失败了,任何人都可以建议我这样做的安全方法,仍然允许用户使用其操作系统的标准对话框自己设置文件名?

我确实尝试在 save() 之前调用 FileReference.browse(),如

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

<小时>

编辑 2:

在 Mac OS 和 Windows 上针对 Chrome 的 FP 11.8 修复.

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

<小时>

添加 navigateTo 解决方法信息

有执行 navigateTo() 的 php 选项:将文件发布到 mime 类型为 image/jpeg 的 php 页面 如果您指定正确的标题,您可以让浏览器调出浏览器的本机保存对话框,该对话框确实维护扩展.但是,该方法还有其他缺点:

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

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.

解决方案

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.

这篇关于在 FileReference.save() AS3 上锁定文件类型的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆