我知道Mobile Safari不支持<input type="file" />。如果用户无法通过我的HTML表单上传文件,我希望能够显示某种“不支持”的信息。
我浏览了this question,虽然BK的答案是好的,但它并不是决定性的。
根据设备宽度移除窗体是不是更明智?我的意思是用@media (max-device-width: 480px) {}测试设备宽度。这是一种糟糕的方法吗?市场上是否有支持直接在浏览器中上传文件的移动设备?
我知道iOS6 will support media uploads,但它还没有发布。还有其他的吗?Android怎么样?Windows Mobile?
发布于 2012-09-18 23:41:02
我刚试过这个..。它起作用了。
你自己试试吧!http://fiddle.jshell.net/nmGRu/show/ (如果你发现任何浏览器没有报告正确的结果,我很想知道...同样,对于任何额外的工作,它将有助于完成此答案)
Safari (iOS 5及更低版本)将返回false,因为它不支持文件上传(特别是它允许您添加输入,但将其标记为禁用)……然而,支持它的移动浏览器,如三星Galaxy Tab (安卓),BlackBerry PlayBook / BlackBerry 10 (我正在Dev Alpha上测试)将返回true,因为它们的浏览器确实支持上传。
到目前为止,测试结果是正确的:
H231Samsung Galaxy Nexus )(检测support)Samsung Galaxy Nexus (检测support)Samsung Galaxy Nexus7平板电脑)检测support)Samsung Galaxy Note (检测support)Samsung Galaxy S2 (检测support)Samsung Galaxy S2)(检测support)Tizen Galaxy S3 (检测支持)到目前为止不正确的检测测试结果:
Windows Phone {}(检测到支持,但实际上没有支持)注意:我正在对此代码进行修订,以解决windows phone上的检测问题
这是一个干净的版本,它只返回一个布尔值...并且不会污染页面。
function hasFileUploadSupport(){
var hasSupport = true;
try{
var testFileInput = document.createElement('input');
testFileInput.type = 'file';
testFileInput.style.display = 'none';
document.getElementsByTagName('body')[0].appendChild(testFileInput);
if(testFileInput.disabled){
hasSupport = false;
}
} catch(ex){
hasSupport = false;
} finally {
if(testFileInput){
testFileInput.parentNode.removeChild(testFileInput);
}
}
return hasSupport;
}
alert(hasFileUploadSupport());https://stackoverflow.com/questions/12479897
复制相似问题