Question drop link from chrome

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
how do i catch a drag and drop link from chrome? I can catch a file drop from the desktop just fine, but dragging from chrome doesnt seem to work.
 
I just did a test using Edge and I would assume that Chrome would work the same way. Set AllowDrop to True on your form or control, handle its DragEnter event and add this code:
VB.NET:
For Each format As String In e.Data.GetFormats()
    MessageBox.Show(e.Data.GetData(format)?.GetType().ToString(), format)
Next
Run the project and drag a link onto your form/control. If you see what I saw, you should be able to take it from there. If you don't see what I saw, that suggests that Chrome isn't playing nice with standard Windows functionality.
 
When I drag a link from chrome, I get a bunch of system.io.memorystream and system.string
 
I just dragged a link from chrome address bar and the for loop give me a bunch of those messages in the msgbox. Even some blanks. 23 messages from 1 drop.
 
So what's the problem? Obviously there is data there and in 23 different formats, so you need to look at each option and decide which one works best for you and then use it. This is how drag'n'drop works. You check whether data is available in a format you want to use and, if it is, you get the data in that format and use it. If you got 23 messages then the data is available in 23 different formats. In the code I posted, e.Data.GetData(format) gets the data available in the specified format. Which format provides data that is most useful to you? That's the format you should use. It works exactly the same ways as getting a file dragged from File Explorer, except it may be a different format from the FileDrop you're using in that case.
 
how do you get the data from that? With the filedrop from desktop, i catch it in dragdrop and that doesnt fire for link drop. I did a
CType(e.Data.GetData(DataFormats.FileDrop), String())
to get the file names but that doesnt work for the link
 
Like I said, you're not going to be able to use FileDrop as the format because the browser doesn't make the data available in that format. I've shown you how to see which formats are available and how to get the data in each of those formats. You need to examine the data in each case, determine which format you think the data you get is most useful and write your code to look for that format and, if it's available, get the data in that format. You said earlier that you get MemoryStream and String as data types so I would guess that you can read the file contents from that stream or else use the URI in the string to get the file.

If you're still having trouble, please specify a web page and the link that you're trying to drag and drop and I'll have a closer look.
 
Back
Top