How to determine presence of HTML5 drag'n'drop file upload API (like the one from FF3.6)

前端 未结 8 667
一生所求
一生所求 2020-12-01 04:53

I am writing an application that\'s supposed to support HTML5 drag/drop API for file, much like the on described here. I would like to perform a programmatic check on whethe

相关标签:
8条回答
  • 2020-12-01 05:03

    This code fails in IE8. This would probably be better:

    if (typeof(FileReader) === 'function' && Modernizr.draganddrop) {
      //sexy drag and drop action
    } else {
       //no drag and drop support available :(
    };
    
    0 讨论(0)
  • 2020-12-01 05:06
    if ("files" in DataTransfer.prototype) {...}
    
    0 讨论(0)
  • 2020-12-01 05:11

    Its a bit trickier. iOS7 reports that it supports both FileReader and draganddrop pictures uploading. Since I was looking for a more general file upload that I couldn't do with iOS, I needed another answer.

    Modernizr issue 57 at here talks about the issue. Now with windows 8 and both touch and mouse, its tricky. There's code in the middle by chriskeeble that I used successfully. It relies on Modernizr and agent detection.

    0 讨论(0)
  • 2020-12-01 05:14
    if (!window.File || !window.FileReader || !window.FileList || !window.Blob) {
      alert('The File APIs are not fully supported in this browser. Please upgrade your browser.');
    }
    
    0 讨论(0)
  • 2020-12-01 05:15

    I had to make a slight change to dshaw's answer for support in IE8:

    if (!!window.FileReader && Modernizr.draganddrop) {
      // sexy drag and drop action
    } else {
      // no drag and drop support available :(
    }
    

    You can try it out here

    0 讨论(0)
  • 2020-12-01 05:17

    either use pure Modernizr approach

    if (Modernizr.filereader && Modernizr.draganddrop) {
      //sexy drag and drop action
    } else {
       //no drag and drop support available :(
    };
    

    or use underlying DOM check directly but with exception handling

    var featuresSupported = false;
    try {
       if (!!(window.File && window.FileList && window.FileReader) && Modernizr.draganddrop)
         featuresSupported = true;
    )
    catch (err)
    {
    }
    
    if (featuresSupported)
      <do sexy effects>
    else
      <perform rollback to legacy stuff>
    
    0 讨论(0)
提交回复
热议问题