将图片从其他网站拖放到我的网站 [英] Drag and Drop images from another website to mine
问题描述
我跑了一个快速的google和so搜索,发现类似的问题,但没有一个是良好的形式,大多数是旧的,看起来被遗弃(没有答案,没有一段时间的意见)。所以这里是...
I ran a quick google and SO search and found similar questions but none were well formed and most were old and looked abandoned (no answers, and no comments for a while). So here goes...
我想要能够收集从另一个网站被丢弃到我的网站上的图像的url(只有url)..我有两个chrome窗口打开,窗口A有我的应用程序,窗口B有它的imgur我打开一个图像点击并拖动到我的窗口,放开我现在我需要知道的图像上的url页)。
I want to be able to collect the url (only the url) of an image being dropped onto my site from another website.. (i.e. I have two chrome windows open. Window A has my application in it. Window B has imgur in it. I open an image click and drag it to my window and let go. Now I need to know the url of the image dropped on my page).
这是我使用的本地文件的代码。
Here is the code I was working with for local files.
$(document).on('drop', function(e) {
var data = e.dataTransfer || e.originalEvent.dataTransfer;
console.log(data); // data.files is empty
e.preventDefault();
return false;
});
再次,我不想上传任何东西..我不想做任何想象。 ..我只需要知道图像被丢弃在另一个网站的页面上的位置。
Again I do not want to upload anything.. i'm not trying to do anything fancy... I just need to know the location of the image being dropped on the page from another website.
推荐答案
尝试以下操作: http: /jsfiddle.net/2Jet2/70/
$(document).on('dragover', function(e) {
e.preventDefault();
});
$(document).on('drop', function(e) {
e.preventDefault();
e.originalEvent.dataTransfer.items[0].getAsString(function(url){
alert(url);
});
});
我得到http://static3.flattr.net/thing/image/9/4/5/5/0/huge.png?1326712342
当我从另一个浏览器窗口拖动图片时。
I get "http://static3.flattr.net/thing/image/9/4/5/5/0/huge.png?1326712342"
When I dragged that image from another browser window.
.getAsString
接受一个回调,一旦它被调用
.getAsString
takes a callback which gets the url as argument once it's called
在firefox上无效
Doesn't work on firefox
这篇关于将图片从其他网站拖放到我的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!