IE 11和IE Edge获取file.type失败 [英] IE 11 and IE Edge get file.type failed

查看:205
本文介绍了IE 11和IE Edge获取file.type失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 change .btn-file:file:getfileinfo,

getfileinfo:function(e){

var fileInput = document.getElementById('fileupload');
var file = fileInput.files [0]; $!
$ b if(!file ||(file.type!='text / plain'&& file.type!='text / csv'&&&file.type!='application / csv'&& file.type!='application / vnd.ms-excel'&&&file.type!='text / csv')){
$('。alert-file' )。显示();
$('。alert-file')。delay(4000).fadeOut('slow');
返回false;
}
var reader = new FileReader();

reader.onload = function(e){
$('#fileData')。val(reader.result);
$('#fileName')。val(file.name);

};
reader.readAsDataURL(file);

$ b

file.type 为空。所有其他浏览器工作正常。



有人可以帮我解决这个问题吗?

解决方案

在免费的Windows 7 / IE 11虚拟机上重现此错误(这里可以得到),这个关键是不带默认PDF处理程序,所以不知道mimetype是什么。



因此,我想出的唯一解决方法就是这个(作为PDF文件的示例 - 根据需要更改应用程序/ pdf和pdf):

<$ (file.type =='application / pdf'|| file.name.toLowerCase()。endsWith('pdf'))
{
//代码执行
}

注意这个如果没有在所有版本的IE的endsWith上使用polyfill,将不起作用 - 所以这里是你需要包含在代码中的任何地方的polyfill代码片段(来自 Mozilla page here

  if(!String.prototype.endsWith){
String.prototype.endsWith = function(searchString,position){
var subjectString = this.toString();
if(typeof position!=='number'||!isFinite(position)|| Math.floor(position)!== position || position> subjectString.length){
position = subjectString 。长度;
}
position - = searchString.length;
var lastIndex = subjectString.lastIndexOf(searchString,position);
返回lastIndex!== -1&& lastIndex ===位置;
};
}


"change .btn-file :file"       : "getfileinfo",

getfileinfo: function(e){

    var fileInput = document.getElementById('fileupload');
    var file = fileInput.files[0];

    if(!file || (file.type != 'text/plain' && file.type != 'text/csv' && file.type != 'application/csv' && file.type != 'application/vnd.ms-excel' && file.type != 'text/csv')){
        $('.alert-file').show();
        $('.alert-file').delay(4000).fadeOut('slow');
        return false;
    }
    var reader = new FileReader();

    reader.onload = function(e) {
        $('#fileData').val(reader.result);
        $('#fileName').val(file.name); 

    };
    reader.readAsDataURL(file);

}

file.type is null if i used in IE11 and IE Edge. All other browser working fine.

Can someone help me on this ?

解决方案

I was able to reproduce this error on the free Windows 7 / IE 11 VM (available here), which crucially does NOT come with a default PDF handler and so does not know what the mimetype is.

As a result, the only workaround I came up with was this (as an example for PDF files - change application/pdf and pdf as needed):

if(file.type == 'application/pdf' || file.name.toLowerCase().endsWith('pdf'))
{
    //code to execute
}

Note that this won't work without the polyfill for endsWith on all versions of IE - so here is the polyfill snippet which you need to include anywhere in your code (which comes from the Mozilla page here)

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(searchString, position) {
      var subjectString = this.toString();
      if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
        position = subjectString.length;
      }
      position -= searchString.length;
      var lastIndex = subjectString.lastIndexOf(searchString, position);
      return lastIndex !== -1 && lastIndex === position;
  };
}

这篇关于IE 11和IE Edge获取file.type失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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