Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing drag-drop from Chrome on my .NET Windows form

Google Chrome has a handy feature where I can click a download link and drag it into a Windows Explorer window, and then drop. After dropping, Chrome then downloads the file and it appears where I dropped it.

I would like to be able to drop from Google Chrome into my application, but it seems it isn't so simple. I have a DataGridView called gridFiles, and the following code:

Private Sub gridFiles_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles gridFiles.DragDrop
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        Dim DroppedFiles() As String = e.Data.GetData(DataFormats.FileDrop)
        If Not DroppedFiles Is Nothing Then
            For Each file As String In DroppedFiles
                MsgBox(file)
            Next
        End If
    End If
End Sub

Private Sub gridFiles_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles gridFiles.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If

End Sub

When I drop files onto it from Windows Explorer, all works fine and I get a message box for each file that was dropped. However, when I drop from Chrome, nothing happens. The reason for this is that DroppedFiles is equal to Nothing. It seems that e.Data.GetData isn't returning anything. I have checked the formats with e.Data.GetFormats() and it returns FileDrop, FileName, FileNameW as expected with any file drop.

What I am quite sure is happening is that Chrome says it has some files so that the DragEnter functions, but since it hasn't downloaded the file yet, DragDrop cannot be done, so Chrome returns no files. I suspect that in a Windows Explorer context, Chrome somehow gets that window's path and copies the file there itself later.

So my question is...

How can I fool Google Chrome into dropping into my application? I see this working by somehow giving Chrome a temporary folder where it thinks it has dropped the file, and my application would monitor that folder for new files and pull them in once they are downloaded. I just need to find a way for Chrome to "know" that folder.

Alternatively, if I could get the URL of what was dropped, that would be just fine as well. I could then download the file with my program.

Any and all advice is much appreciated. Thanks.


EDIT: So it seems that with regular URLs, I do get the proper dragged-in UniformResourceLocator format. The behavior I am seeing occurs with the download links in Gmail. It probably happens elsewhere, but I am not sure. When a gmail attachment is dragged from Gmail into my application, I get a FileDrop.

Doing some more digging, it seems that Gmail is using the download_url attribute of the anchor tag. I have never heard of this before. Perhaps this is just an extra property they have added?

In any case, since my application will primarily be used with e-mail attachments, I need a way for the phantom FileDrop to work, as stated above. I am unable to use Spy++. It doesn't seem to show any messages when drops occur. (I welcome any advice on that problem as well.)

Edit #2: Here is more information on how Gmail utilizes drag/drop for files: http://www.thecssninja.com/javascript/gmail-dragout

like image 686
Brad Avatar asked May 25 '11 14:05

Brad


People also ask

How do I enable drag in Chrome?

Just hold on your left mouse button, performing a drag-n-drop, yeah, links open in the new tab. With CTRL button holding on, drag-n-drop, your web page will be opened in an inactive state. The drag-to-search feature is also useful and friendly to use.

How do I drag and drop in Chrome?

To drag and drop using the Chromebook touchpad: Click the item you want to move with one finger. With a second finger, move the item. Release both fingers to drop the item at its new location.

Why can't I drag and drop in Chrome?

Fixing Drag-and-DropType "Touch" into the search bar and set the following options to "Enabled". You will have to relaunch Google Chrome for these setting changes to apply. Once you have relaunched Google Chrome, drag-and-drop gestures should now work on most compatible websites.


1 Answers

I've pulled kilometers of hair out of my head myself regarding weird drag-drop behaviour. Spy++ as mentioned by sixlettervariables might be a good idea. Another would be to take a look at the discussion from one of my Q/As here at SO:

Drag and Drop between Instances of the same Windows Forms Application

From my own experience I seem to remember that drag/drop from a browser is a security issue and thus handled differently. Hope this helps.

EDIT:

Maybe this answers your question:

http://www.vbforums.com/showthread.php?t=529211

like image 100
Pedery Avatar answered Oct 23 '22 05:10

Pedery