Cannot get file data from the clipboard using Flex

好久不见. 提交于 2020-02-25 04:30:26

问题


Given: A Flex TileList with the following event:

<mx:nativeDragDrop>
  <![CDATA[
    if(event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
      var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

      for each(var file:File in files)
      {
        // file.data is null here!
      }

      this.listData.refresh();
    }
  ]]>
</mx:nativeDragDrop>

I am trying to create a list of thumbnails from jpegs that I drag into this TileList. Image.source can use the url to show the image, but I need to scale the image down first (hi rez photos) I already have the scaling part done except that I need BitmapData from the file and it has null for file.data.

ALSO, I have tried this:

var x:URLRequest = new URLRequest(value.file.url); // this is a local file (e.g. file:///C:/somefile.jpg)
var b:Bitmap = new Bitmap(x.data as BitmapData);

data is ALSO null! So frustrating. Any help would be appreciated.


回答1:


I assume this is a part of an AIR application. (Accessing the clipboard from a plain Flex app is not possible.)

I have no experience with AIR, but your second code block is clearly wrong. An URLRequest instance does nothing in itself, it is but a static object storing the request details. In order to fetch the data from that URL, you need to create a Loader, and pass the request to that loader like this:

var req:URLRequest = new URLRequest(value.file.url); // this is a local file (e.g. file:///C:/somefile.jpg)
var ldr:Loader = new Loader();
ldr.addEventListener(Event.COMPLETE, function(event:Event):void {
   var b:Bitmap = event.target.content as Bitmap;
});
ldr.load(req);

Of course, you'd have to fill in the Event.COMPLETE handler. Note that the Loader class can be used to load SWF and image objects, for all everything else, you'd have to use URLLoader and parse the data yourself.

Regarding the nativeDragDrop block, here's a snippet from the documentation:

Typically a handler for the nativeDragEnter or nativeDragOver event evaluates the data being dragged, along with the drag actions allowed, to determine whether an interactive object can accept a drop. To specify that an interactive object is an eligible target, the event handler must call the NativeDragManager.acceptDrop() function, passing in a reference to the object. If the user releases the mouse button over the designated object, the object becomes the drop target and dispatches the nativeDragDrop event.

Are you calling NativeDragManager.acceptDrop() properly?



来源:https://stackoverflow.com/questions/542664/cannot-get-file-data-from-the-clipboard-using-flex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!