file可以上传文件,但通常 情况下大家都会需要设置文件上传的格式
上传文件的格式由一个 accept 属性来控制 列如:
<input type="file" id="file" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" name="uploadFile"/>
上面这个是只可以上传excel 表格限制的设置方式,两个格式之间用 , 隔开
常用的一些类型
后缀名 MIME名称
*.3gpp audio/3gpp, video/3gpp
*.ac3 audio/ac3
*.asf allpication/vnd.ms-asf
*.au audio/basic
*.css text/css
*.csv text/csv
*.doc application/msword
*.dot application/msword
*.dtd application/xml-dtd
*.dwg image/vnd.dwg
*.dxf image/vnd.dxf
*.gif image/gif
*.htm text/html
*.html text/html
*.jp2 image/jp2
*.jpe image/jpeg
*.jpeg image/jpeg
*.jpg image/jpeg
*.js text/javascript, application/javascript
*.json application/json
*.mp2 audio/mpeg, video/mpeg
*.mp3 audio/mpeg
*.mp4 audio/mp4, video/mp4
*.mpeg video/mpeg
*.mpg video/mpeg
*.mpp application/vnd.ms-project
*.ogg application/ogg, audio/ogg
*.pdf application/pdf
*.png image/png
*.pot application/vnd.ms-powerpoint
*.pps application/vnd.ms-powerpoint
*.ppt application/vnd.ms-powerpoint
*.rtf application/rtf, text/rtf
*.svf image/vnd.svf
*.tif image/tiff
*.tiff image/tiff
*.txt text/plain
*.wdb application/vnd.ms-works
*.wps application/vnd.ms-works
*.xhtml application/xhtml+xml
*.xlc application/vnd.ms-excel
*.xlm application/vnd.ms-excel
*.xls application/vnd.ms-excel
*.xlt application/vnd.ms-excel
*.xlw application/vnd.ms-excel
*.xml text/xml, application/xml
*.zip aplication/zip
*.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
file文件里面还有一个重要的属性 required 设置未上传文件时不能上传,建议只有一个提交按钮时使用,如果有两个按钮,只需限制其中一个时候,可以使用js来完成 html:
<form action="#" th:action="@{/hosts}" method="post" enctype="multipart/form-data" class="hostform">
<input type="file" id="file" accept="application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" name="uploadFile"/>
<input type="submit" value="导入" name="import" class="imp"/>
<input type="submit" value="导出" name="export"/>
<br><br>
</form>
js:
$(".imp").attr({"disabled":"disabled"});
var file = $("#file").val();
$("#file").change(function(){
if (file == null||file == ""){
$(".imp").removeAttr("disabled");
return;
}
})
这里使用到了disabled 属性 ,它是用来禁用input ,注 disabled属性无法与<input type="hidden">一起使用。