html5 判断文件类型,常见文件的mine type类型及获取方法

前面提到Kangle 服务器添加 mine type 文件类型,但是网上给出的文件类型难免没有那我全面,往往我们正需要的网上却没有。

下面介绍一个简单的 mine type 文件类型的获取方式 ,利用 JS 获取文件的类型:

首先我们写一个 input 的文件上传按钮,HTML:

然后利用 JS 获取到该按钮,监听他的 onchange 事件,打印出文件的 mine type 文件类型:

var input = document.getElementById("input")

input.onchange = (e) => {

console.log(`File type is "${e.target.files[0].type}"`);

}

这样就能在控制台打印出所选文件的 mine type 文件类型了:

6310a07162b43d83df7bb08e85519edd.png

为了更直观的表现,可以直接打印在前台页面,下面是优化全部代码:

File type is :

var input = document.getElementById("input")

var mineType = document.getElementById("mineType")

input.onchange = (e) => {

console.log(`File type is "${e.target.files[0].type}"`);

mineType.innerHTML = '"'+e.target.files[0].type+'"';

}

效果如下:

b1545cdb33ee19b7b99e758c0cfeb110.png

常见的 mine type 文件类型:

MIME typeFile extensionsapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx

application/msworddoc

application/pdfpdf

application/rtfrtf

application/vnd.ms-excelxls

application/vnd.ms-powerpointppt

application/x-rar-compressedrar

application/x-shockwave-flashswf

application/zipzip

audio/midimid midi kar

audio/mpegmp3

audio/oggogg

audio/x-m4am4a

audio/x-realaudiora

image/gifgif

image/jpegjpeg jpg

image/pngpng

image/tifftif tiff

image/vnd.wap.wbmpwbmp

image/x-iconico

image/x-jngjng

image/x-ms-bmpbmp

image/svg+xmlsvg svgz

image/webpwebp

text/csscss

text/htmlhtml htm shtml

text/plaintxt

text/xmlxml

video/3gpp3gpp 3gp

video/mp4mp4

video/mpegmpeg mpg

video/quicktimemov

video/webmwebm

video/x-flvflv

video/x-m4vm4v

video/x-ms-wmvwmv

video/x-msvideoavi

参考文献:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值