简体   繁体   English

测试浏览器是否支持多个文件上传?

[英]Testing if a browser supports multiple file uploads?

Is there a way to test if a given browser supports the multiple attribute on file upload elements? 有没有办法测试给定的浏览器是否支持文件上传元素的multiple属性? Either a server-side or client-side implementation is sufficient. 服务器端或客户端实现就足够了。

I do realize I can test the user-agent against a list of known browsers that support the feature, but that seems like a rather frail implementation (ie. if IE 10 supports the feature when it finally launches, the I'll have to go edit back my code). 我确实知道我可以针对支持该功能的已知浏览器列表测试用户代理,但这似乎是一个相当脆弱的实现(即如果IE 10最终启动时支持该功能,我将不得不去编辑我的代码)。 I'd prefer to test support of the feature directly. 我更愿意直接测试该功能的支持。

Thoughts? 思考?

Since this functionality is part of the HTML5 specification and is only just emerging in current implementations, you may not yet have a definitive and reliable way to do this. 由于此功能是HTML5规范的一部分,并且仅在当前实现中出现,因此您可能还没有明确可靠的方法来执行此操作。 Want to know for sure? 想知道吗? Then test it on as many browsers as possible. 然后在尽可能多的浏览器上测试它。 However, with that said, the code segment found at: 但是,话虽如此,代码段位于:

https://developer.mozilla.org/en/DOM/Input.multiple https://developer.mozilla.org/en/DOM/Input.multiple

does show that you should be able to determine this based on a simple test for the existence of the "multiple" attribute like is commonly used in many other elements. 确实表明你应该能够根据对“多重”属性的存在的简单测试来确定这一点,就像许多其他元素中常用的一样。

我建议"multiple" in document.createElement("input")使用"multiple" in document.createElement("input")进行特征检测。

There is no reliable way to do this at present time, as has been evidenced by Viljami Salminen's tests of file upload support on mobile browsers (ie support for input type=file - let alone for multiple file upload). 目前没有可靠的方法可以做到这一点, Viljami Salminen在移动浏览器上对文件上传支持的测试证明了这一点 (即支持input type=file - 更不用说文件上传)。 His findings show that many browsers/devices falsely report support . 他的研究结果表明,许多浏览器/设备都错误地报告支持

What I mean by "no reliable way to do this at present time" is that we don't have a list of false positives for this particular feature, the HTMLInputElement.multiple property. 我的意思是“目前没有可靠的方法” ,我们没有这个特殊功能的误报列表,即HTMLInputElement.multiple属性。 Obviously the list would include the entire regexp of false positives for input type=file , but would likely also include additional false positives just for this particular feature - hence the ability to detect this capability awaits a systematic test of browsers. 显然,该列表将包括input type=file的整个误报正则 ,但可能还包括仅针对此特定功能的额外误报 - 因此检测此功能的能力等待浏览器的系统测试。

Temporarily, you could use the following test, based entirely on Salminen's code, with the addition of one line ( el.multiple = true; ), and substituting xxx for the regexp referenced above. 暂时,您可以完全基于Salminen的代码使用以下测试,添加一行( el.multiple = true; ),并用xxx代替上面引用的正则表达式。

var isMultipleFileInputSupported = (function () {
    // Handle devices which falsely report support
    if (navigator.userAgent.match(/xxx/)) {
        return false;
    }
    var el = document.createElement('input');
    el.type = 'file';
    el.multiple = true;
    return !el.disabled;
})();

Usage example: 用法示例:

if (isMultipleFileInputSupported) {
    console.log('multiple file input is supported');
} else {
    console.log('multiple file input is NOT supported');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM