HTML Drag and Drop interfaces enable web applications to drag and drop files on a web page. This document describes how an application can accept one or more files that are dragged from the underlying platform's file manager and dropped on a web page.
The main steps to drag and drop are to define a drop zone (i.e. a target element for the file drop) and to define event handlers for the drop and dragover events. These steps are described below, including example code snippets. The full source code is available in MDN's drag-and-drop repository (pull requests and/or issues are welcome).
Note that HTML drag and drop defines two different APIs to support dragging and dropping files. One API is the DataTransfer interface and the second API is the DataTransferItem and DataTransferItemList interfaces. This example illustrates the use of both APIs (and does not use any Gecko specific interfaces).
Typically, an application will include a dragover event handler on the drop target element and that handler will turn off the browser's default drag behavior. To add this handler, you need to include a ondragover event handler:
The drop event is fired when the user drops the file(s). In the following drop handler, if the browser supports DataTransferItemList interface, the getAsFile() method is used to access each file; otherwise the DataTransfer interface's files property is used to access each file.
Drag and Drop (DnD) is one of the many great features of HTML 5, and it is supported in Firefox 3.5, Safari, Chrome and IE. Google recently rolled out a new feature that allows Google Chrome users to drag and drop files from the browser to the desktop. It is an extremely convenient feature, but it was not widely known until Ryan Seddon posted an article on the discoveries of his reverse engineering on this new feature.At Box.net, we are very excited about how these new capabilities are enabling us to improve our cloud content management solution, as well as contribute more to the developer community. I am pleased to announce that DnD Download has been integrated into our product. Now, Box users can drag files directly from a Chrome browser to their desktop to download and save the file.I would like to share how I went through several iterations during the development of this new feature.Check for Drag and Drop API Support #The first thing to do is check that your browser fully supports HTML5 drag and drop. An easy way to do that is use a library called Modernizr to check for a certain feature:
I first tried the approach that Seddon found in Gmail. I added a new attribute called 'data-downloadurl' to anchor links of files. This process uses HTML5's custom data attributes. In data-downloadurl, you need to include the MIME type of the file, the destination file name (the desired file name of the downloaded file), and the download URL of the file. Thus, this is added to the HTML template:
But I prefer feature detection to browser detection, though this technically does not detect that DnD download will work.Problems of iteration 1 #Because we currently have on-page DnD enabled for moving/copying files between folders, we need a way to distinguish DnD Download and on-page DnD. Technically, we cannot combine these two actions. We cannot predict whether the user wants to move a file to another folder within the Box.net account or drag it to their desktop. These two actions are completely different. Moreover, there is no easy way to detect if the cursor is outside the browser window. You could use window.onmouseout (IE) and document.onmouseout (other browsers) to attach mouseout event to the document, and check if e.relatedTarget.nodeName == "HTML" (e is the mouseout event or window.event, whichever is available). But this is quite difficult due to event bubbling. The event may trigger randomly when you are over an image or layer, especially in a complex web app like Box.net.
We want the user to explicitly do something to prevent them from dragging something out to the desktop by mistake. Potentially, an editor of a Box folder can upload an executable file that does something undesirable on the computer of whoever downloads it. We want the user to know exactly when a file would be downloaded to the desktop.
We decided to experiment with control + drag (dragging a file when the Windows Ctrl key is pressed). This action is consistent with what people can do on a Windows desktop to duplicate a file. It also requires extra work (but not an extra step) from the user to prevent files from downloaded by mistake.The jQuery plugin in iteration 1 is abandoned now because we need to tightly integrate DnD Download with the on-page DnD. For those who are interested, we use a modified version of jQuery UI's Draggable plugin. Inside the mousedown event of a target element, we put the following code:
Other than enabling the Ctrl key, we also added a little toaster tooltip, which shows up when the user performs a regular on-page drag. It tells the user that files can be downloaded if the file icon is dragged to the desktop while the Ctrl key is being held.Problems of iteration 2 #Due to security concerns, Box.net does not expose permanent URLs to directly access static files. This is not unique to Box.net. Any online storage service should not expose permanent URLs without an extra layer of security to check if the file is public and whether the intended download is requested by a user with appropriate permissions.When following the "download URL" (e.g. _download_file?file_id=f_60466690) of an item, it returns a "302 Found" status code, and redirects to a random URL (e.g. =1f1207a084&m=168299,11211&t=2&b=aca15820d924e3b) that is the temporary "actual URL" of the file. The challenge is that it expires every few minutes, and so placing it in the HTML output is impractical. It could return "404" when the user tries to download the file at the link in the HTML output generated several minutes ago.DnD Download only works on actual URLs that point directly to a resource. If redirection is involved, it is currently not smart enough to follow the chain (and it should never follow the chain due to security). Therefore, although the link _download_file?file_id=f_60466690 from above would let you download the file when you enter it in the browser location bar, it would not work with DnD.To better illustrate the differences between an "actual URL" and a "redirect URL", see the screenshots:302 redirect URLActual URLIteration 3 #Let's try Ajax.We slightly modified the code in the previous iteration and came up with the following:
This makes sense. Upon dragstart, it immediately makes an Ajax call to the server to retrieve the latest download URL of the file. However, it does not work.It turns out that it needs to be a synchronous call (or as I like to call it, Sjax). Seems like setData has to be done at the time when the event listener is attached. According to jQuery's API, the highlighted lines become:
There is no cross-browser way to upload dragged & dropped files without Ajax. Some browsers (IE and Firefox) do not allow setting the value of a file input, which then could be submitted to server in a usual way.
This is essentially for IE 9-. We do not need to collect the dragged & dropped files because in this case (isAdvancedUpload = false), the browser does not support drag & drop file upload and the form relies only on the input[type="file"].
If you have a simple form with only a drag & drop area or file input, it may be a user convenience to avoid requiring them to press the button. Instead, you can automatically submit the form on file drop/select by triggering the submit event:
We had to use both dragenter and dragover for the highlighting because of what I mentioned earlier. If you start off hovering directly over dropArea and then hover over one of its children, then dragleave will be fired and the highlight will be removed. The dragover event is fired after the dragenter and dragleave events, so the highlight will be added back onto dropArea before we see it being removed.
If you want to add multiple images from your computer at once to your Divi Builder page, simply select all the images you want to use on your computer and drag them over into the Divi Builder. Divi will automatically upload all the images, convert them into an image gallery module, and create a new section displaying your new image gallery. You can rename your files before dragging them into your page, in order for them to display custom titles.
The JSON file extension is used for dragging complete page layouts or sections into any Divi page. Dragging in a JSON file automatically loads the content into the page. When importing JSON files, you can choose to overwrite the content on an existing page, or start from scratch with a blank page. 2b1af7f3a8