Wednesday, May 20

Drag and Drop file data (Fun with JS)


Short bit of code which when a user clicks inside a webpage, and while continuing holding down the mouse button, drags outside of the web browser, we create a file. This will create a file all on the client side, no server monkey business required. Tested and works in Chrome.

Code

body = document.getElementsByTagName('body')[0];

body.setAttribute('draggable',true);

body.addEventListener("dragstart", function (e) { e.dataTransfer.setData( "DownloadURL", "text/html:filename.html:data:text/html;utf8," + "hello world" ); });

0 comments :

Post a Comment