none of Firefox (34.0.5) and Chrome (39.0.2171.95 (64-...">

Why is Firefox or Chrome not detecting the .json file type?

For <input type="file">

none of Firefox (34.0.5) and Chrome (39.0.2171.95 (64-bit)) can determine the type of .json file. You can try it with the following code:

<!DOCTYPE html>
<html>
<head>
  <title>Test</title>
</head>

<body>

  <input type="file" id="file_input" name="files[]" multiple />

<script type='text/javascript' src='lib/jquery-1.9.1.js'></script>
<script>
$("#file_input").on('change', function(evt) {

  var files = evt.target.files; 
  //files is a FileList of File objects.
  var i;
  var len = files.length;

  for (i=0; i < len; ++i) {
    console.log(files[i]);
    console.log("Type: " + files[i].type);
  }
})
</script>


</body>
</html>

      

+3


source to share


3 answers


I got around the lousy browser support for the file type .json

with the following code:

    #coffeescript:

    file_type = file.type

    if file_type is ''  #then possibly a .json file, `is` => ===
      [..., file_ext] = file.name.split '.'   #If file.name is 'json', then file_ext will be 'json' as well.
      if file_ext is 'json'
        file_type = 'application/json'   

      



... then I used file_type

instead of file.type

eg.

         headers: {
            'Content-Type': file_type, 
            ...
          },

      

+2


source


You can refer to How the mime type of an uploaded file is determined by the browser? ... It checks the filename of the file extension first, so the * .json file will have file.type

like application/json

. If not found, it will ask the system for the file type, make sure your download is recognized as a json file. According to https://developer.mozilla.org/en-US/docs/Web/API/Blob.type it would be empty if unknown.



+1


source


This is simply because the browser cannot determine its MIME type using an algorithm . The specification states that if the type cannot be determined, an empty string must be returned. You should never answer a property type

as it will not be accurate.

0


source







All Articles