0

What do I mean by a semi-image? (if you have a better wording for it-suggestions are welcomed)

Well, as an example- go to Google Images (it's the same for many other websites as well) and search for "cat". Dragging and dropping the first item onto my droppable area gives just one URL which isn't exactly one of an image, for instance- https://www.google.com/imgres?imgurl=https%3A%2F%2Fr.hswstatic.com%2Fw_907%2Fgif%2Ftesla-cat.jpg&imgrefurl=https%3A%2F%2Fanimals.howstuffworks.com%2Fpets%2Fteslas-cat-and-other-feline-fascinations.htm&docid=yTPSYBCDHgIU0M&tbnid=GTJT8CqjTKEtwM%3A&vet=10ahUKEwiYtpzaiKbdAhUytosKHSXeC4AQMwjhASgAMAA..i&w=907&h=510&bih=937&biw=1855&q=Cat&ved=0ahUKEwiYtpzaiKbdAhUytosKHSXeC4AQMwjhASgAMAA&iact=mrc&uact=8

That kind of "not simply an image" url happens for many different websites- Google Images was just an example (most of the time it actually is an image file and everything is working fine). More specifically,within the drop event handler,

event.dataTransfer.getData("URL");

gives that kind of a url, and

event.dataTransfer.files

is just an empty list for that event.

Now,remember that what the user actually saw was an image being dropped onto the droppable area- so if that won't work on something which is supposed to accept images,that would be a frustrating experience for the user. When I drop those kind of semi-images (from any website) onto Google images droppable area everything is working fine,so there must be a way.

My question is- how do I handle those kind of events and produce a Blob/a File Object from it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yoni Keren
  • 1,170
  • 2
  • 13
  • 24

1 Answers1

1

If you need to download an image dragged into your droppable area, best way to do it is to send the URL to your back-end and download from there (any back-end technology is suitable).

By doing a quick Google search, you can find some related work: [1], [2] and [3].

Here's a small code example showing how that would work in the JavaScript side:

// Get raw HTML from dropped element
var droppedHTML = evt.dataTransfer.getData('text/html');

// Assuming that the dropped element is an image, get the "src" attribute (URL)
var dropContext = $('<div>').append(droppedHTML);
var imgUrl = $(dropContext).find('img').attr('src');

// Now you can call your API via AJAX or some other way
$.post('analyze_image.php',
    {
        imgUrl: imgUrl,
    },
    function(response, status) {
        // Feedback on response
    }
);

Then, in your back-end, assuming you have a PHP server, you can download from there, as showed in this Stack Overflow thread:

file_put_contents($imagePath, fopen($imgUrl, 'r'));
Fabio Manzano
  • 2,847
  • 1
  • 11
  • 23
  • To clarify the question, I've stated that within the drop event handler (so I'm not sure why you put a link to drop events and copy pasted an explanation on something that I'm already using),that's what happens, and Google images results is just an example.Literally follow what I've written and see what you get for yourself, it will take about 5 minutes. I'm getting urls which are not just image files, and I don't know how to handle that. You didn't answer my question at all since you didn't understand it. Would you rephrase anything there? – Yoni Keren Sep 09 '18 at 21:09
  • In the example, the drop event triggers a call to a web API. Are you trying to build an API, having an image as input? – Fabio Manzano Sep 09 '18 at 21:20
  • I'm trying to handle images in a droppable zone on a webpage I'm building(to give you the full context, the user drops an image and then it is being sent to my server to do some analysis with my trained AI algorithm,estimating the gender age IQ smoking habits and shape and so on of the people in the image). That happens for other websites as well- but Google Images seem to be able to grab the image anyways, and I don't know how. What the user sees is an image being dropped there, so I feel like I need to handle that ;) – Yoni Keren Sep 09 '18 at 21:26
  • Can't you receive a URL as a parameter in your API and then start a download from your back-end? – Fabio Manzano Sep 09 '18 at 21:33
  • Everything is possible, but I will have to learn how to handle those kind of urls anyways. If it's in the front end then it's with JavaScript, if it's on my server then it's python or Java or whatever. But how do I do that? How do I grab the image from that kind of a url? That's the question,right?! ;) – Yoni Keren Sep 09 '18 at 21:38
  • Okay ,looking forward to your answer! – Yoni Keren Sep 09 '18 at 21:55
  • Just added new info. Thanks! – Fabio Manzano Sep 10 '18 at 00:33
  • That's much better- I'll try that asap (away for a 2 days). Unless the question needs to be edited first though, I would delete the previous body of the answer ;) – Yoni Keren Sep 10 '18 at 07:35
  • Cool, I think this is working :) Let's make a reasonable effort to leave a decent question and answer. I would leave the answer after the word EDIT. Is there anything you would like to change when it comes to the question (to make it clearer as well?). – Yoni Keren Sep 11 '18 at 18:20
  • @YoniKeren, just edited the answer. I think we're good. Thank you!! – Fabio Manzano Sep 11 '18 at 20:46