JavaScript中是否有一种方法可以检测是否可以将文件拖放到使用的设备上? [英] Is there a way in JavaScript to detect if files can be dropped on the used device?

查看:73
本文介绍了JavaScript中是否有一种方法可以检测是否可以将文件拖放到使用的设备上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上创建了一个表格,允许用户上传文件.用户还可以将文件拖放到此表单上.但是,在某些设备上,根本无法拖放文件(例如,在我的iPhone上).因此,我只想显示文本将文件拖放到此处".仅在有意义的设备上.

I created a form on a website that allows users to upload files. Users can also drag and drop files onto this form. However, on some devices, it is simply not possible to drag and drop files (e.g. on my iPhone). So I only want to display the text "Drag and drop files here" only on devices on which it makes sense.

原则上是否可以检测设备是否支持拖放文件?一种解决方法是检测鼠标指针是否在移动.在这种情况下,我想可以拖放文件.但是,这只是一个猜测.

Is there a way to detect whether the device supports drag and drop files in principle? A workaround could be to detect if a mouse pointer is moving. In this case, I would guess it is possible to drag and drop files. However, it would only be a guess.

有没有更合适的方法来检测到这一点?请注意,我通常不想检测浏览器是否支持拖放,但是如果浏览器支持拖放 files .

Is there a more suitable way to detect this? Please note that I don't want to detect if the browser supports drag and drop in general, but if the browser supports drag and drop files.

推荐答案

您可以使用window.navigator.userAgent来检测用户所使用的操作系统.userAgent返回一个字符串,您可以解析该字符串以尝试检测其中的某些关键字.我在极客(极客)中查看了这篇文章.一个>.连同此stackoverflow答案

You could use window.navigator.userAgent in order to detect what OS the user is using. userAgent returns a string and you could parse through the string to try and detect certain keywords inside of it. I looked at this article at geeks for geeks. Along with this stackoverflow answer here. by combining the two you should be able to pick out certain keywords and detect what the user is on for most cases. It seems that older phones might have an issue with this so it may not be perfect.

if (navigator.appVersion.indexOf("Mac") != -1) {
  onWeb = true;
} 
if (navigator.appVersion.indexOf("Win") != -1) {
  onWeb = true;
} else {
onWeb = false;
}

我个人认为(请带一点盐),其中涉及的工作量以及旧设备上的潜在错误都不值得进行.因此,我将尝试使用文本通知用户拖放的可能性,并为他们提供一个上传文件的按钮.一些用户更喜欢通过拖放(我自己)手动上传文件的功能,并且通过随时使用这两个选项,它允许用户利用他们最喜欢的选择.

In my personal opinion (please take this with a grain of salt) the amount of work involved in this with the potential errors on older devices isn't worth the work involved. Because of this I would try to use text to inform the user of the Drag and Drop potential as well as giving them a button to upload files. Some users prefer the ability to manually upload files over a drag and drop (myself), and by having both options readily available it allows users to utilize which ever choice they prefer most.

这篇关于JavaScript中是否有一种方法可以检测是否可以将文件拖放到使用的设备上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆