问题
Can we detect whether a browser supports dropping a file over an <input type="file" />
?
For example, this is possible in Chrome but not in IE8.
Modernizr.draganddrop
is a possibility but is it the right choice? I'm not adding any custom drag/drop event handlers.
Update
To verify Joe's answer here's a jQuery example that should stop the file drop. Verified in Chrome and Firefox.
$yourFileInput.on('drop', function() {
return false; // if Joe's explanation was right, file will not be dropped
});
回答1:
I think the answer to Detecting support for a given JavaScript event? may help you. Adjusting the code to test against Input instead of Div, and testing for the "Drop" event seems to work fine for me.
Reproduced here so you don't have to click through (and adjusted slightly, since it seems you only need to detect this one feature):
function isEventSupported(eventName) {
var el = document.createElement('input');
eventName = 'on' + eventName;
var isSupported = (eventName in el);
if (!isSupported) {
el.setAttribute(eventName, 'return;');
isSupported = typeof el[eventName] == 'function';
}
el = null;
return isSupported;
}
if(isEventSupported('drop')){
//Browser supports dropping a file onto Input
} else {
//Fall back, men!
}
来源:https://stackoverflow.com/questions/13086400/feature-detection-for-ability-to-drop-file-over-html-file-input