2016-02-29 15 views
1

我試圖從Google Chrome或Internet Explorer中拖動圖像並將其放入我的Flex項目中,但無法直接從圖像中獲取圖像temp文件夾,如Mozilla火狐,如何從AS3中的谷歌瀏覽器或Internet Explorer中拖放圖像時獲取圖像

比如我現在用的是下面的代碼去獲得由系統臨時文件夾拖放圖片在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最初存儲圖像的位置。

有沒有人有一個想法,它存儲或如何解決這個問題?

+0

從瀏覽器中拖動圖像不發送的文件列表,它會發送位圖或文本/網址 – BadFeelingAboutThis

+0

是的,Chrome一段時間後打破了。您曾經獲得圖片,但Chrome改變了一些東西(我注意到其他應用無法通過拖放操作從Chrome中加載圖片)。您可以使用URL格式將其加載到您的AIR應用中。 – Aaron

回答

4

我只是嘗試這樣做,當你從Chrome中通過AIR應用程序拖動圖像時,它就會出現,格式如下:

  1. 文件承諾列表
  2. 網址
  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; 
    } 
+0

感謝您的「BadFeeling關於此」爲您的寶貴答案 – Ashish

+0

我的問題是在以下鏈接:http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_image_link在上面的鏈接,當我從Mozilla拖拽笑臉Firefox它工作正常(即我在nativePath變量中獲得像「C:\ Users \ User_Name \ AppData \ Local \ Temp \ ubeh2wbl.bmp」的路徑),但在Chrome中,當我拖放到項目中時, nativePath變量和url中的NULL我只能得到http://www.w3schools.com/,那麼你能告訴我爲什麼它會發生這樣的事情。 – Ashish

+0

我不能告訴你爲什麼(Chrome開發人員必須這樣做),但可能它會給你錨點href,因爲圖像被包裝在一個錨點中,這對URL屬性是有意義的。以你的具體例子來說,我注意到'HTML_FORMAT'爲你提供了整個節點html,你可以使用它來解析出src值。要知道它是如何一致的,需要一定的實驗。我會用我最好的猜測來更新我的答案。 – BadFeelingAboutThis

相關問題