如果我在 AS3 中从 Google Chrome 或 Internet Explorer 拖放图像,如何获取图像

How to get image if I drag and drop an Image from Google Chrome or Internet Explorer in AS3

我正在尝试从 Google Chrome 或 Internet Explorer 中拖放图像并将其放入我的 Flex 项目中,但我无法直接从 Mozilla Firefox 等临时文件夹中获取图像,

例如,在 Mozilla Firefox 的情况下,我使用以下代码从系统临时文件夹中获取拖放图像:

this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onNativeDragDrop);

private function onNativeDragDrop(vEvent:NativeDragEvent)
{
    var dropFiles:Array = vEvent.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

    if(dropFiles && dropFiles.length > 0)
    {
        var original:File = File(dropFiles[0]);
        var nativePath:String = original.nativePath;
    }
}

在 nativePath 中,我得到图像最初存储的路径,如:"C:\Users\User_Name\AppData\Local\Temp\ubeh2wbl.bmp"

但是在 Google Chrome 或 Internet Explorer 的情况下,我在 nativePath 中得到 NULL

所以我不知道 Google Chrome 或 Internet Explorer 最初存储图像的位置。

有没有人知道它存储在哪里或如何解决这个问题?

我刚刚尝试过,当您将图像从 Chrome 拖到 AIR 应用程序上时,它会以以下格式出现:

  1. 提交承诺清单
  2. url
  3. 正文
  4. html

文件承诺列表为空,因此我们将重点关注其他内容。

这是我使用的代码,它通过多种格式回退以尝试获取拖动的图像或图像路径:

    private function onNativeDragDrop(e:NativeDragEvent):void
    {
        var img:DisplayObject; //the image (or first image) that was dragged

        //IF IT'S A BITMAP (The easiest, so check it first)
        if (e.clipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT)) {
            var bmd:BitmapData = BitmapData(e.clipboard.getData(ClipboardFormats.BITMAP_FORMAT));

            img = new Bitmap(bmd);
            addChild(img);
            trace("It's a bitmap");
        }

        //IF IT'S FILE(S) that are dragged, try this next
        if (!img && e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {

            var dropfiles:Array;
            var file:File;

            dropfiles = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
            for each (file in dropfiles)
            {
                //isImagePath is defiend below
                if (isImagePath(file.nativePath)) {
                    img = loadImage(file.nativePath); //load image function is defined below
                    break; //let's just load the first image
                }
            }

        }

        //IF IT's A URL that was dragged, try this next
        if (!img && e.clipboard.hasFormat(ClipboardFormats.URL_FORMAT)) {
            var url:String = String(e.clipboard.getData(ClipboardFormats.URL_FORMAT));
            trace("It's a url: ", url);
            if (isImagePath(url)) {
                trace("it's a URL image path");
                img = loadImage(url);
            }

        }

        //IF IT's HTML that was dragged, try this next
        if (!img && e.clipboard.hasFormat(ClipboardFormats.HTML_FORMAT)) {
            var html:String = String(e.clipboard.getData(ClipboardFormats.HTML_FORMAT));
            trace("its HTML: ", html);

            //use regex to get all the <img> tags from the html
            var imgs:Array = html.match(/(<img.*?>)/g);

            //if one or more was found
            if (imgs.length) {
                trace("Image tag(s) found");
                var imgPath:String;
                for (var i:int = 0; i < imgs.length; i++) {
                    //use regex to get the src value from the img tag
                    imgPath = imgs[i].match(/src="(.*?)"/)[1];
                    img = loadImage(imgPath);
                    break; //let's just load the first image
                }
            }
        }

        //IF IT's raw text that dragged, try this next
        if (!img && e.clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) {
            var txt:String = String(e.clipboard.getData(ClipboardFormats.TEXT_FORMAT));
            trace("its text: ", txt);

            if (isImagePath(txt)) {
                img = loadImage(txt);
            }
        }

    }

    private var imgTypes:Vector.<String> = new < String > ["jpg", "gif", "png"];
    private function isImagePath(path:String):Boolean {
        if (path.lastIndexOf(".") > -1) {
            var ext:String = path.substr(path.lastIndexOf(".")).toLowerCase();
            return new RegExp(imgTypes.join("|")).test(ext);
        }

        return false;
    }

    private function loadImage(path):Loader {
        var l:Loader = new Loader();
        l.load(new URLRequest(path));
        addChild(l);
        return l;
    }