Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得HTML5的window.fileReader API的跨浏览器兼容性_Javascript_Html - Fatal编程技术网

Javascript 如何获得HTML5的window.fileReader API的跨浏览器兼容性

Javascript 如何获得HTML5的window.fileReader API的跨浏览器兼容性,javascript,html,Javascript,Html,嗨,Html5开发者和HTML4开发者也 我想知道是否有办法在IE8及以上版本中支持html5的window.fileReader API 代码如下: if(window.fileReader){ alert("supported"); // in html5 works and fires alert but not in IE8 } 实际上,我想在提交之前阅读文件的内容。 如果支持window.fileReader,我可以使用一些条件吗 使用HTML5遵循“A”行代码,否则如果不支

嗨,Html5开发者和HTML4开发者也

我想知道是否有办法在IE8及以上版本中支持html5的window.fileReader API

代码如下:

if(window.fileReader){
  alert("supported"); // in html5 works and fires alert but not in IE8 
}
实际上,我想在提交之前阅读文件的内容。

  • 如果支持window.fileReader,我可以使用一些条件吗
    使用HTML5遵循“A”行代码,否则如果不支持window.fileReader,则
    使用其他东西…读取文件
  • 我还想知道不使用
    HTML5文件API。。。但不使用ActiveXObject

等待响应。

您始终可以使用try-catch检查浏览器中是否支持FileReader()

try {
    var reader = new FileReader();
    //do something
}
catch(e) {
    alert("Error: seems File API is not supported on your browser");
    //do something
}

您可以使用简单的if逻辑检查功能,并将您的功能包装在那里

if ('FileReader' in window) {
  // Do something with FileReader since it's supported
} else {
  // Do something with your polyfill solution
}
您可以尝试像这样的polyfill解决方案(适用于IE8):

您需要检查多个部件的支持情况:

//Full File API support.
if ( window.FileReader && window.File && window.FileList && window.Blob )
{
    //Supported
}

else alert( "Not supported" );
你的第二个要求:

我还想知道不使用 HTML5文件API。。。但不使用ActiveXObject

你能做的是:

  • 有一个iframe(你将发布到这个)
  • 具有“文件”类型的输入
  • 当用户选择本地文件时,您可以使用javascript将其发布到iframe(例如
    document.getElementById(“myForm”).target=“myIframe”;document.getElementById(“myForm”).submit();
  • iframe的发布页面(可以是PHP)读取上传的文件内容并回调父页面(例如
    window.parent.someCallbackFunction(theFileCOntents);