Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to drag+drop a file from the browser to the desktop, causing a download?

I have an app that stores files and uses drag+drop to upload them. Is there a way I can drag+drop to download the file to my desktop instead of having to click "download." Basically, the opposite of drag+drop upload.

like image 398
Matthew Berman Avatar asked Mar 23 '11 20:03

Matthew Berman


People also ask

What happens when you drag and drop a file?

By default, if you left-click and HOLD the left mouse or touchpad button while moving your mouse pointer to a different folder location on the same drive, when you release the left mouse button the file will be moved to the new location where you released the mouse button.

What happens when you drag a file into Chrome?

Drag File to Chrome. You can drag files from your computer and drop on the open Chrome window to view the file on the browser. For example, you can do this with images, text files and videos instead of searching application to open the files.

How do you drag a file to your desktop?

To drag and drop a file or folder, click it with your left mouse button, then, without releasing the button, drag it to the desired location and release the mouse button to drop it. Refer to your Windows help for more information if you haven't used drag and drop.

Why is my drag and drop not working 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.


2 Answers

The html5rocks and cssninja are ok, but I think way overkill for quick answers. Here is a simple example of using drag events from something, including to download files.

<style>
div { background-color: #eee; border: 1px solid black; padding: 5px; float: left; clear: both; }
</style>
<div id="uiLinkText"           draggable="true">Drag <b>Text</b> to a text editor </div>
<div id="uiLinkHyperlink"      draggable="true">Drag <b>Hyperlink</b> to address bar </div>
<div id="uiLinkUrlDownload"    draggable="true">Drag <b>Url Download</b> to file system </div>
<div id="uiLinkStaticDownload" draggable="true">Drag <b>Static Download</b> to file system </div>
<script>
document.getElementById('uiLinkText').ondragstart = function(event){
  // plain text, for dropping into text editor
  event.dataTransfer.setData('text/plain', 'Go to http://stackoverflow.com/questions/5411481/ to read about this.');
}
document.getElementById('uiLinkHyperlink').ondragstart = function(event){
  // URL, for dropping into the browser's address bar
  event.dataTransfer.setData('text/uri-list', 'http://stackoverflow.com/questions/5411481/');
}
document.getElementById('uiLinkUrlDownload').ondragstart = function(event){
  // file download contents, for dropping into a file system
  event.dataTransfer.setData('DownloadURL', 'text/plain:SourceQuestion.html:http://stackoverflow.com/questions/5411481/')
}
document.getElementById('uiLinkStaticDownload').ondragstart = function(event){
  var textToWrite = 'file contents here';
  var textFileAsBlob = new Blob([textToWrite], { type: 'text/xml' });
  var url = window.URL.createObjectURL(textFileAsBlob);
  // file download contents, for dropping into a file system
  event.dataTransfer.setData('DownloadURL', 'text/plain:Static.txt:' + url)
}
</script>

Warning: While this worked fine for me in Chrome locally (in Windows 7), when I tried putting it on jsfiddle for a linke, the "Static Download" did not work, and the "Url Download" crashed Google Chrome.

As with most drag-and-drop, it does not work with MSIE 9, I have not tried 10+ or Firefox.

like image 136
Abacus Avatar answered Sep 27 '22 19:09

Abacus


It is supported in Google Chrome only.
http://www.html5rocks.com/en/tutorials/dnd/basics/#toc-desktop-dnd-out For example it is implemented in Gmail.

None of any other browsers support this behavior.

like image 34
IT Hit WebDAV Avatar answered Sep 27 '22 18:09

IT Hit WebDAV