語法
var mimetype = instanceOfFile.type
值
一個字串。
範例
var i, fileInput, files, allowedFileTypes;
// fileInput 是個 HTMLInputElement: <input type="file" multiple id="myfileinput">
fileInput = document.getElementById("myfileinput");
// files 是個 FileList 物件 (類似 NodeList)
files = fileInput.files;
// 這範例接受 *.png, *.jpeg 和 *.gif 圖片。
allowedFileTypes = ["image/png", "image/jpeg", "image/gif"];
for (i = 0; i < files.length; i++) {
// 測試 file.type 是否是允許的類別。
if (allowedFileTypes.indexOf(files[i].type) > -1) {
// 若符合則執行這裡的程式碼。
}
});
規格
規格 | 狀態 | 備註 |
---|---|---|
File API The definition of 'type' in that specification. |
Working Draft | 初定義 |
瀏覽器相容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
功能 | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
File.type | 5 | (Yes) | 4.0 (2) | 10.0 | 11.10 | 5.1 |
功能 | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
File.type | No support | (Yes) | No support | No support | No support | No support |