Check if variable holds File or Blob
W3.org:
'A File object is a Blob object with a name attribute, which is a string;'
In case of File:
var d = new Date(2013, 12, 5, 16, 23, 45, 600);
var generatedFile = new File(["Rough Draft ...."], "Draft1.txt", {type: 'text/plain', lastModified: d});
console.log(typeof generatedFile.name == 'string'); // true
In case of Blob:
var blob = new Blob();
console.log(typeof blob.name); // undefined
Condition:
var isFile = typeof FileOrBlob.name == 'string';
Easiest way:
a = new File([1,2,3], "file.txt");
b = new Blob([1,2,3]);
c = "something else entirely";
a instanceof File
> true
b instanceof File
> false
c instanceof File
> false
One liner solution to make life easier.
Based on Ric Flair
const isFile = input => 'File' in window && input instanceof File;
const isBlob = input => 'Blob' in window && input instanceof Blob;
const a = new File([1,2,3], "foo.txt", {type: "text/plain"});
...
console.log(isFile(a)) // true
...