Javascript 检查变量是否包含 File 或 Blob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31525667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if variable holds File or Blob
提问by alexandernst
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?
Javascript 有File
和Blob
用于文件表示,两者几乎是一回事。有没有办法检查变量是否保存了一种File
或一种Blob
类型的数据?
采纳答案by Reflective
W3.org:
W3.org:
'A File object is a Blob object with a name attribute, which is a string;'
'File 对象是一个带有 name 属性的 Blob 对象,它是一个字符串;'
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:
在 Blob 的情况下:
var blob = new Blob();
console.log(typeof blob.name); // undefined
Condition:
健康)状况:
var isFile = typeof FileOrBlob.name == 'string';
回答by Nick Brunt
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
回答by Peter Drinnan
This works for me when trying to determine is an object is a file:
当我试图确定一个对象是一个文件时,这对我有用:
var reader = new FileReader();
if(file.type){
reader.readAsDataURL(file);
}
If I want to check if it is a specific file type like an image I do this:
如果我想检查它是否是像图像这样的特定文件类型,我会这样做:
if(file.type && file.type.indexOf('image') !== -1){
reader.readAsDataURL(file);
}
回答by Marcos Kubis
Compare the constructor class:
比较构造函数类:
var b = new Blob()
console.log(b.constructor === Blob) // true
回答by Ric Flair
Based on Nick Brunt's:
基于尼克布伦特的:
function isFile(input) {
if ('File' in window && input instanceof File)
return true;
else return false;
}
function isBlob(input) {
if ('Blob' in window && input instanceof Blob)
return true;
else return false;
}
//Test
const a = new File([1,2,3], "foo.txt", {type: "text/plain"});
const b = new Blob([1,2,3]);
const c = "something else entirely";
console.log(isFile(a)); //true
console.log(isBlob(a)); //true
console.log(isFile(b)); //false
console.log(isBlob(b)); //true
console.log(isFile(c)); //false
console.log(isBlob(c)); //false
Won't work for Opera mini as well as IE. Though with IE it won't matter because you may only use file input tags, in that case you'll definitely know it's a file.
不适用于 Opera mini 和 IE。虽然使用 IE 没有关系,因为您可能只使用文件输入标签,在这种情况下,您肯定会知道它是一个文件。
回答by Ignacio Bustos
One liner solution to make life easier.
一种衬里解决方案,让生活更轻松。
Based on Ric Flair
基于 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
...