无法使用Flex从剪贴板获取文件数据

时间:2022-07-01 01:51:23

Given: A Flex TileList with the following event:

给定:具有以下事件的Flex TileList:

<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.

我正在尝试从jpegs创建一个缩略图列表,我将其拖入此TileList。 Image.source可以使用url来显示图像,但是我需要首先缩小图像(hi rez photos)我已经完成了缩放部分,除了我需要文件中的BitmapData并且file.data为null。

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 个解决方案

#1


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

我假设这是AIR应用程序的一部分。 (无法从普通的Flex应用程序访问剪贴板。)

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:

我没有使用AIR的经验,但你的第二个代码块显然是错误的。 URLRequest实例本身不执行任何操作,它只是一个存储请求详细信息的静态对象。为了从该URL获取数据,您需要创建一个Loader,并将请求传递给该加载器,如下所示:

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.

当然,您必须填写Event.COMPLETE处理程序。请注意,Loader类可用于加载SWF和图像对象,对于其他所有内容,您必须使用URLLoader并自行解析数据。

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

关于nativeDragDrop块,这里是文档的一个片段:

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.

通常,nativeDragEnter或nativeDragOver事件的处理程序会评估正在拖动的数据以及允许的拖动操作,以确定交互式对象是否可以接受删除。要指定交互式对象是符合条件的目标,事件处理程序必须调用NativeDragManager.acceptDrop()函数,并传入对象的引用。如果用户在指定对象上释放鼠标按钮,则该对象将成为放置目标并调度nativeDragDrop事件。

Are you calling NativeDragManager.acceptDrop() properly?

你正确调用NativeDragManager.acceptDrop()吗?

#1


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

我假设这是AIR应用程序的一部分。 (无法从普通的Flex应用程序访问剪贴板。)

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:

我没有使用AIR的经验,但你的第二个代码块显然是错误的。 URLRequest实例本身不执行任何操作,它只是一个存储请求详细信息的静态对象。为了从该URL获取数据,您需要创建一个Loader,并将请求传递给该加载器,如下所示:

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.

当然,您必须填写Event.COMPLETE处理程序。请注意,Loader类可用于加载SWF和图像对象,对于其他所有内容,您必须使用URLLoader并自行解析数据。

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

关于nativeDragDrop块,这里是文档的一个片段:

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.

通常,nativeDragEnter或nativeDragOver事件的处理程序会评估正在拖动的数据以及允许的拖动操作,以确定交互式对象是否可以接受删除。要指定交互式对象是符合条件的目标,事件处理程序必须调用NativeDragManager.acceptDrop()函数,并传入对象的引用。如果用户在指定对象上释放鼠标按钮,则该对象将成为放置目标并调度nativeDragDrop事件。

Are you calling NativeDragManager.acceptDrop() properly?

你正确调用NativeDragManager.acceptDrop()吗?