问题描述
我正在尝试从Google Chrome或Internet Explorer拖动图片并将其拖放到我的Flex项目中,但是我无法从Mozilla Firefox的temp文件夹直接获取图像,
例如我正在使用以下代码,以获取Mozilla Firefox中的系统临时文件夹拖放图像:
这个.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onNativeDragDrop);
私有函数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最初存储图像的位置。有没有人知道存储在哪里或如何解决这个问题?
我刚刚尝试过,当您通过AIR应用程序从Chrome拖动图像时,它的格式如下:
- 文件承诺清单
- url
- 文字
- html
文件承诺列表为空,因此我们将专注于其他。
这里是我使用的代码,通过几种格式,可以尝试获取拖动的图像或图像路径:
private function onNativeDragDrop(e:NativeDragEvent):void
{
var img:DisplayObject; //拖动的图像(或第一个图像)
//如果它是一个BITMAP(最简单的,请先检查)
if(e.clipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT )){
var bmd:BitmapData = BitmapData(e.clipboard.getData(ClipboardFormats.BITMAP_FORMAT));
img = new Bitmap(bmd);
addChild(img);
trace(这是一个位图);
}
//如果被拖动的文件,请尝试下一个
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;
(文件在dropfiles中)
{
// isImagePath is defiend below
if(isImagePath(file.nativePath)){
img = loadImage(file。的nativePath); // load image function under
break; //让我们加载第一个图像
}
}
}
//如果它是一个被拖动的URL,请尝试下一个
if(!img&& e.clipboard.hasFormat(ClipboardFormats.URL_FORMAT)){
var url:String = String(e.clipboard.getData(ClipboardFormats.URL_FORMAT));
trace(这是一个url:,url);
if(isImagePath(url)){
trace(这是一个URL图像路径);
img = loadImage(url);
}
}
//如果被拖动的HTML,请尝试下一个
if(!img&& e.clipboard)。 hasFormat(ClipboardFormats.HTML_FORMAT)){
var html:String = String(e.clipboard.getData(ClipboardFormats.HTML_FORMAT));
trace(其HTML:,html);
//使用regex来获取所有的< img>来自html
var imgs的标签:Array = html.match(/(< img。*?>)/ g);
//如果找到一个或多个
if(imgs.length){
trace(Image tag(s)found);
var imgPath:String;
for(var i:int = 0; i< imgs.length; i ++){
//使用regex从img标签获取src值
imgPath = imgs [i] .match(/ SRC = (*)。?/)[1];
img = loadImage(imgPath);
break; //让我们加载第一个图像
}
}
}
//如果它的原始文本被拖动,请尝试下一个
if(! img& e.clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)){
var txt:String = String(e.clipboard.getData(ClipboardFormats.TEXT_FORMAT));
trace(它的文本:,txt);
if(isImagePath(txt)){
img = loadImage(txt);
}
}
}
private var imgTypes:Vector。< String> = new<字符串> [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;
}
私有函数loadImage(路径):Loader {
var l:Loader = new Loader();
l.load(new URLRequest(path));
addChild(l);
return l;
}
I am trying to drag an Image from Google Chrome or Internet Explorer and Drop into my Flex Project but I am unable to get Image directly from temp folder like Mozilla Firefox,
For Example I am using the following codes go get drag drop images from system temp folder in case of 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;
}
}
In nativePath i am getting the path where the image is initially stored like : "C:\Users\User_Name\AppData\Local\Temp\ubeh2wbl.bmp"
But In case of Google Chrome or Internet Explorer I am getting NULL in nativePath.
So I Don't know where Google Chrome or Internet Explorer initially storing the images.
Does anyone have an idea where it is storing or how to solve this problem ?
I just tried this, and when you drag an image from Chrome over the an AIR app, it comes in as the following formats:
- file promise list
- url
- text
- html
The file promise list is empty, so we'll focus on the others.
Here is the code I used which falls back through several formats to try and get a dragged image or image path:
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;
}
这篇关于如果我在AS3中从Google Chrome或Internet Explorer中拖放图像,如何获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!