简体   繁体   English

JavaScript中的本地文件支持检测-Windows Phone 8错误

[英]Local File Support Detection in JavaScript - Windows Phone 8 Bug

I have a question about <input type=file> HTML element -- specifically, why all signs point to it being supported on Windows Phone 8 (IE 10 Mobile), yet it really doesn't work. 我有一个关于<input type=file> HTML元素的问题-具体来说,为什么所有迹象都表明Windows Phone 8(IE 10 Mobile)支持该元素,但它实际上不起作用。

I have been working on a photo uploader project, and in the Javascript development, I have logic in place to check for File API support, so I know if I can support local file selection. 我一直在从事照片上传器项目的开发,在Javascript开发中,我有适当的逻辑来检查File API的支持,因此我知道我是否可以支持本地文件选择。 Surprisingly, the code I have in place is returning a true value on Windows Phone 8. This shouldn't be an issue, however the <input type=file> element isn't really supported for WP 8 (as noted on the Windows blog in an article about IE 10 for Mobile ). 令人惊讶的是,我所拥有的代码正在Windows Phone 8上返回true值。这应该不是问题,但是WP 8确实不支持<input type=file>元素(如Windows博客所述)在有关IE 10移动版文章中 )。 The result on a WP 8 is if an <input type=file> element is on the page, it shows up just fine, but nothing happens when clicking the "Browse" button. WP 8上的结果是,如果页面上有<input type=file>元素,则显示的很好,但是单击“浏览”按钮时没有任何反应。

My JS code to check File API support is: 我检查文件API支持的JS代码是:

function checkLocalFileSupport() {
   if (window.File && window.FileReader && window.FileList && window.Blob) {
       return true;
   }
   return false;
}

I'm hoping someone could shed some light on how to better check and confirm if local file selection is available on a specific device/browser. 我希望有人可以阐明如何更好地检查和确认在特定设备/浏览器上是否可以使用本地文件选择。 As I said, my current solution returns true on WP 8. I've been trying a variety of different methods to see if an <input type=file> is enabled (snippets below), but all of the other solutions still return true for WP 8. 正如我所说的,我目前的解决方案返回true的WP 8我一直在尝试各种不同的方法来查看是否<input type=file>已启用(以下片段),但所有其他解决方案还是回到true的工作组8。

One caveat is this: I really want the code to be device-agnostic. 一个警告是:我真的希望代码与设备无关。 As such, I don't want to have to sniff for a browser user-agent. 因此,我不想嗅探浏览器用户代理。 In my mind, user-agent sniffing is a fallback that I'd rather not have to implement. 在我看来,用户代理嗅探是一种后备,我宁愿不必实施。

Here are a few solutions I've found and tried, yet still return true on WP 8: 这是我找到并尝试过的一些解决方案,但在WP 8上仍然返回true

Referencing the disabled attribute on the input element: 引用输入元素上的disabled属性:

function isInputTypeFileImplemented() {
    var elem = document.createElement("input");
    elem.type = "file";
    if (elem.disabled) return false;
    try {
        elem.value = "Test"; // Throws error if type=file is implemented
        return elem.value != "Test";
    } catch(e) {
        return elem.type == "file";
    }
}

Using jquery.support: 使用jquery.support:

var support = (function(undefined) {
    return $("<input type='file'>")    // create test element
                 .get(0)               // get native element
                 .files !== undefined; // check whether files property is not undefined
})();

Hopefully someone can help shed some light on how to accurately get WP 8 to return a false value for local file support. 希望有人可以帮助您阐明如何准确地获取WP 8以返回false值以获取本地文件支持。

Thanks in advance! 提前致谢!

According to this blog post some systems return false positive support and you can overcome this by checking them with RegExp such as 根据此博客文章,某些系统返回了误报支持,您可以通过使用RegExp检查它们来克服此问题,例如

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;
}

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

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