Giter VIP home page Giter VIP logo

dropfile's Introduction

What is dropfile.js

dropfile.js is a shim/polyfill for adding support for dragging files from the desktop to IE browsers prior to IE10 and FF prior to 3.6 browsers

Demo http://adodson.com/dropfile/

Video http://vimeo.com/18326844

<iframe src="http://player.vimeo.com/video/18326844" width="400" height="300" style="float:right;" frameborder="0"></iframe>

How to use dropfile.js

  1. Save dropfile.js and dropfile.xap into the same directory on your site.

  2. Add to the bottom of you page the dropfile.js script <script src="somepath/dropfile.js"></script>

  3. Add HTML5 code for handling the action on an element, here i'm attaching event handling to my #holder element.

     window.onload = function () {
         var holder = document.getElementById('holder');
         holder.ondragover = function () { return false; };
         holder.ondragenter = function () { return false; };
         holder.ondrop = function (e) {
             e = e || window.event;
     
             // Read from e.files, as well as e.dataTransfer
             var files = (e.files || e.dataTransfer.files);
     
             var s = "";
             for (var i = 0; i < files.length; i++) {
                 (function (i) {
                     var reader = new FileReader();
                     reader.onload = function (event) {
                         holder.innerHTML = "<li><img src='" + event.target.result + "' /> " + (files[i].name) + "</li>" + holder.innerHTML;
                     };
                     reader.readAsDataURL(files[i]);
                 })(i);
             }
     
             return false;
         };
     }
    

Working with jQuery

jQuery and other API's, wrap the original javascript event object and makes it available at e.originalEvent. This script does not recognise jQuery but does recognise the ondrop event that was assigned by jQuery and will execute that when used. However now the original event object is passed to the handler not the wrapped up event object used in jQuery. But not to worry its easy to write code which can accommodate both approaches. See http://mrswitch.github.com/dropfile/demo-jquery.htm

Browser Support

Browser IE7 IE8 IE9 IE10 FF3.5 FF3.6 Chrome 6+ Safari 5 (win) Safari 6 (win) Opera 10
File API / dropfile dropfile File API dropfile File API File API dropfile File API no1
  1. Silverlight does not run in Opera, nor does Opera support the File API

Contribute

  1. MIT license, so anyone can use it, modify it, and add it to their own application.
  2. "source" folder contains the Visual Studiio Solution files if you fancy modifying the code

How it works

This fix works by "polyfilling" browsers which dont support the File API ... aka, they also dont expose file data when a file drop triggers a dropevent.

We take advantage of silverlight which does support the dragging files in. So to add this to native HTML elements in the background a dragenter event gets hijacked by a silverlight widget which sneaks under the mouse cursor and picks up the file data. The Silverlight widget then interacts with javascript, transfering data about the file(s) being dropped, which then triggers the ondrop drop event on the element.

Break down of code

  1. Checks whether the browser needs has no support for the File API and
  2. Adds a silverlight object to the page and hides it by positioning it outside the window view
  3. Adds ondragenter event to the window.document and performs the following actions:
  4. The silverlight widget follows the mouse cursor
  5. Creates window.dropfile in the scope of the element.
  6. When files gets dropped on to the Silverlight widget, the Silverlight widget passes file data over to window.dropfile which triggers the ondrop event on that element in scope - An example of which is in the sample code below.

Why it was done this way, rather than...

  1. Why not, Make the Silverlight object transparent over element.

    answer: The Silverlight object needs to be in "windowless" mode for transparency, however in this mode we lose the drag and drop UI. See http://msdn.microsoft.com/en-us/library/cc838156(VS.95).aspx

  2. Why not, Add drop functionality implictly to objects

    answer: So this is the Event Delegation vs Event Handling argument. Using Event Delegation we add this to the document ondragenter event and explicitly add this to elements which match a condition as the user drags over elements, thus avoiding having to add functional code.

  3. Why not, Update e.dataTransfer.files instead of having to write (e.files||e.dataTransfer.files)

    answer: The Event object dataTransfer is immutable therefore we can only pass in a bespoke method. This is only for IE8 and less, in IE9 we can define the dataTransfer files.

  4. Why not... Call the widget on just the elements which have an ondrop event, rather than the whole doc.

    answer: initially i required elements to have a flag to say, yes this has an ondropevent because there is no way to find out if they do (i.e. "Event delegation"). And it's too much hassle to add classNames, i guess i could add a strict mode, but i wanted a script that could be "dropped in", if you'll pardon the pun.

To Do

Shim up <input type="file" onchange="handleFiles(this.files)" >. Aka.. add file references to the file object + trigger te onchange event.

dropfile's People

Contributors

brunopessanha avatar cmcculloh avatar jonathantneal avatar mrswitch avatar orthographic-pedant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dropfile's Issues

Error on reading large files

The script breaks when I drop files that are larger than approx. 75MB.
It exits with the following error:

SCRIPT5022: Unhandled Error in Silverlight 2 Application [ScriptObject_InvokeFailed]

Arguments:

Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=8.0.60831.0&File=System.Windows.Browser.dll&Key=ScriptObject_InvokeFailed
at System.Windows.Browser.ScriptObject.Invoke(String name, Object[] args)
at DropFile.MainPage.LayoutRoot_Drop(Object sender, DragEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

license?

Can you add a license to your project please? (Like MIT or something... )

I'd love to use this in a project, but, I work for a big company and lawyers etc etc...

I'm happy to submit a pull request if you don't want to do the work :)

jquery demo does not work with ie

Hi there,

the jquery demo needs to be updated to ignore also the dragenter event. After adding that the demo still does not work because
both e.files and e.dataTransfer.files are undefined.

Hosted files in Chrome with the File:// extension do not support FileReader()

According to the chrome security guidelines, you can't use filereader on local files while using chrome with the file accessed via the file:// protocol. This causes the plugin to fail silently.

Steps to Repro:

  1. Open up demo.htm on chrome by using file->open. (or by dropping it into the window)
  2. Drag and drop from your desktop.

Expected:
Normal behaviour. File is dropped/etc.

Actual:
Fails silently.

FileReader.readAsText(file) doesn't work

I'm trying to implement this polyfill in safari 5.1 on Mac and FileReader.readAsText(file) doesn't work.
the property file.data (line 214 in dropfile.js) is undefined.

IE9 issue

Hello,

I run into the following issue:

I try the solution using silverlight and it works well in a simple web app [Image case_ok], the silverlight object appears on dragging-dropping and does his job but when I tried to implement the same solution on a large application (it has backbone and a whole other things) the silverlight object does not appear [Image case_not_ok] and then the error of "file property is null or undefined" appears on console.

Both tests are running in at the same time so the IE configuration is the same for both cases.

I will appreciate all the help possible. Thanks.

Pdta: excuse my terrible english.

[case_not_ok]
case_not_ok

[case_ok]
case_ok

SilverLight undentified error !

Hi MrSwitch ,

I'm really excited in this project , I try to build it in my asp.net page , but when I run page , I got this error :

"Microsoft JScript runtime error: 'Silverlight' is undefined"

in the line : document.getElementById("holder").innerHTML = "Ouch this browser doesn't support FileReader or the shim. " + (!Silverlight.isInstalled() ? "
Please install Silverlight" : "");

It does not work on IE anymore :( Sorry, I'm new , please tell me how to solve that !

Thanks in advance !

valentinoNguyen !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.