3

For an <input type="file"> neither Firefox(34.0.5) nor Chrome(39.0.2171.95 (64-bit)) can determine a .json file's type. 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>
7stud
  • 46,922
  • 14
  • 101
  • 127

3 Answers3

2

I've circumvented the lousy browser support for the .json file type 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, e.g.

         headers: {
            'Content-Type': file_type, 
            ...
          },
7stud
  • 46,922
  • 14
  • 101
  • 127
1

You may refer to How is mime type of an uploaded file determined by browser?. It first checks the file extension name, so a *.json file will have file.type as application/json. If not found, it will enquiry the system for the file type, make sure your upload file is recognized as json file. According to https://developer.mozilla.org/en-US/docs/Web/API/Blob.type, it would be empty if unknown.

Community
  • 1
  • 1
zuo
  • 1,061
  • 1
  • 12
  • 27
  • **According to https://developer.mozilla.org/en-US/docs/Web/API/Blob.type, it would be empty if unknown** Yes, I am fully aware of that. Better docs here: https://developer.mozilla.org/en-US/docs/Web/API/File – 7stud Dec 17 '14 at 05:28
  • 1
    *so a *.json file will have file.type as application/json* The reason I posted my question is because a .json file in fact does not have a file.type equal to 'application/json'. – 7stud Dec 18 '14 at 06:19
0

It's simply because the browser can't determine its MIME type through its algorithm. The specification states that if the type cannot be determined, then an empty string must be returned. You should never reply on the type property since it will not be accurate.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • How is it possible for a browser not to know the type of a .json file? My file's got valid JSON in it, and its extension is .json. I humbly suggest that Google might consider using a different algorithm. – 7stud Dec 17 '14 at 05:04
  • @7stud - Why is it not possible? How does it know it is JSON inside but not only plain text? – Derek 朕會功夫 Dec 17 '14 at 05:06
  • there really is no standard json file format, like there is for gif, js, css, or html. it grew up as an interchange format, not a data archive format. what app does any computer come with designed to open json files? – dandavis Dec 17 '14 at 05:14
  • @Derek朕會功夫, ...because the extension is .json? – 7stud Dec 17 '14 at 05:25
  • @7stud - That's not how type works. There's more than just the "json" text. Though .json should really be a standard these days and Chrome should know it's application/json – Derek 朕會功夫 Dec 17 '14 at 05:26
  • By the way, that algorithm has nothing to do with determining a file's type. – 7stud Dec 18 '14 at 06:24