开发者

Can Action Script FileReference object be sent to Javascript and converted to File object?

开发者 https://www.devze.com 2023-01-06 18:33 出处:网络
In Action Script, FileReference.browse() is called. I would like to send the chosen file to Javascript and convert it to File object, just like if I would press the Browse button of the <input typ

In Action Script, FileReference.browse() is called.

I would like to send the chosen file to Javascript and convert it to File object, just like if I would press the Browse button of the <input type='file /> and got File object.

I need that to be able to display picture thumbnail like shown here.

Maybe instead of passing the wh开发者_C百科ole object it is possible to send only the required information for the thumbnail from the object ?

Thanks !


I don't think there is any way to create a File object. For example, try this:

var f = new File;

I get an exception "Cannot convert WrappedNative to function." If you look in the docs for File, there is no constructor described. Also, most of the attributes are read-only. I would interpret this to mean that only the browser can create a File object.

Keep in mind that if you could create a File object by specifying the path to a file, it would be easy to upload files without the user's consent. That would be a serious security hole.

You may find it more practical to use only Flash or only HTML5 for this.


For people trying to do it. What can be done is:

  1. Converting the fileReference data byterray to Base64 and put it in a string
  2. Sending this string to javascript with ExternalInterface

I use this pattern in contexts where I want to have an overview of binary files content. Even a large image can be sent using this approach. The file can then be accessed in javascript using a data URI with the appropriate MIME:

data:image/png;base64,[data] or data:octet/stream,[data]

Since I'm posting almost 2 1/2 years after the first post, we can now do it in all popular browser.

0

精彩评论

暂无评论...
验证码 换一张
取 消