如何检测浏览器是否支持文件上传? (移动设备和桌面设备)

发布于 2025-01-05 20:55:53 字数 115 浏览 3 评论 0 原文

我正在开发适用于移动和桌面浏览器的应用程序。我想知道是否有办法检测浏览器是否支持文件上传。我正在专门寻找功能检测而不是浏览器检测。有什么办法可以查到吗?

服务器端或客户端都可以。

谢谢

I'm developing an application for both mobile and desktop browsers. I'm wondering if there is anyway to detect if a browser supports file uploading. I'm looking specifically for feature detection and not browser detection. Is there any way to find out?

Server-side or client side is fine.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

芸娘子的小脾气 2025-01-12 20:55:53

客户端 JavaScript:

<input type="file" name="file" value="" id="fileUploadField1">      
<script type="text/javascript" charset="utf-8">
    if (document.getElementById('fileUploadField1').disabled)
        { document.write('your device does not allow uploads');     }
        else
        { document.write('your device does allow uploads');     }
</script>

client side javascript:

<input type="file" name="file" value="" id="fileUploadField1">      
<script type="text/javascript" charset="utf-8">
    if (document.getElementById('fileUploadField1').disabled)
        { document.write('your device does not allow uploads');     }
        else
        { document.write('your device does allow uploads');     }
</script>
乖乖哒 2025-01-12 20:55:53

您可能有兴趣阅读这篇有关移动设备上当前输入类型=文件支持以及如何检测它的文章:http://viljamis.com/blog/2012/file-upload-support-on-mobile/(该检测目前经过测试,可在约 120 种不同的移动浏览器/移动操作系统组合上运行)。

基本上,我们只是使用与 Modernizr 类似的检测,但使用 UA 检测作为备份来过滤掉那些错误报告支持的移动浏览器(除了使用这两种方法之外,似乎确实没有其他方法可以可靠地检测它,功能检测浏览器检测)。

You might be interested to read this article about current input type=file support on mobile and how to detect it: http://viljamis.com/blog/2012/file-upload-support-on-mobile/ (the detection is currently tested to be working on ~120 different mobile browser/mobile OS combinations).

Basically, we are just using similar detection as Modernizr does, but use UA detection as a backup to filter out those mobile browsers that falsely report support (there really doesn’t seem to be any other way to detect it reliably than using these both, feature detection and browser detection).

知足的幸福 2025-01-12 20:55:53

您可以使用带有 forms-fileinput 扩展的 Modernizr 框架。尝试一下。

if Modernizr.fileinput
  // you can use file inputs

访问 Modernizr 下载页面 并检查 forms-fileinput 扩展(展开“Non -核心检测”部分)。

You can use Modernizr framework with forms-fileinput extension. Give it a try.

if Modernizr.fileinput
  // you can use file inputs

Visit the Modernizr download page and check the forms-fileinput extension (expand the "Non-core detects" section).

深海里的那抹蓝 2025-01-12 20:55:53

Modernizr 现在支持检查是否支持文件上传。

来自:最好的方法是什么检查用户是否可以上传文件?

if(Modernizr.fileinput) {
    //file input available!
} else {
    //No file input :(
}

如果您担心此库的大小,您始终可以一次获取一个组件 - http://modernizr.com/download/ ...或者您可以复制他们使用的代码:https://github.com/Modernizr/Modernizr/blob/master/feature-detects/forms/fileinput.js

if(navigator.userAgent.match(/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|(Windows Phone (OS 7|8.0))|(XBLWP)|(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|(Kindle\/(1.0|2.0|2.5|3.0))/)) {
    return false;
}
var elem = createElement('input');
elem.type = 'file';
return !elem.disabled;

Modernizr now supports a check for whether or not file uploads are supported.

From: What is the best way to check if user can upload files?

if(Modernizr.fileinput) {
    //file input available!
} else {
    //No file input :(
}

If you're worried about the size of this library, you can always get one component at a time - http://modernizr.com/download/ ... or you can just copy the code they use: https://github.com/Modernizr/Modernizr/blob/master/feature-detects/forms/fileinput.js

if(navigator.userAgent.match(/(Android (1.0|1.1|1.5|1.6|2.0|2.1))|(Windows Phone (OS 7|8.0))|(XBLWP)|(ZuneWP)|(w(eb)?OSBrowser)|(webOS)|(Kindle\/(1.0|2.0|2.5|3.0))/)) {
    return false;
}
var elem = createElement('input');
elem.type = 'file';
return !elem.disabled;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文