Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  26] [ 3]  / answers: 1 / hits: 66571  / 9 Years ago, mon, july 20, 2015, 12:00:00

Javascript has both File and Blob for file representation, and both are almost the same thing. Is there a way to check if a variable is holding a File or a Blob type of data?


More From » fileapi

 Answers
136

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';

[#65742] Friday, July 17, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;