Total Pageviews

Wednesday, 21 October 2020

HTML5 的 input:file 上传类型控制

  一、input:file 属性

属性值有以下几个比较常用:

accept:表示可以选择的文件 MIME 类型,多个 MIME 类型用英文逗号分开,常用的 MIME 类型见下表。

multiple:是否可以选择多个文件,多个文件时其 value 值为第一个文件的虚拟路径。

1、accept

只能选择 png 和 gif 图片.

<input id="fileId1" type="file" accept="image/png,image/gif" name="file" />

2、multiple

多文件上传

<input id="fileId2" type="file" multiple="multiple" name="file" />

3、常用 MIME 类型

后缀名 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


二、样式美化,input上传按钮美化。

我们在做input文本上传的时候,html自带的上传按钮比较丑,如何对其进行美化呢?同理:input checkbox美化,input radio美化是一个道理的,后面文章会总结。

思路:

input file上传按钮的美化思路是,先把之前的按钮透明度opacity设置为0,然后,外层用div包裹,就实现了美化功能。

代码如下:

DOM结构:

<a href="javascript:;" class="a-upload">
    <input type="file" name="" id="">点击这里上传文件
</a>

<a href="javascript:;" class="file">选择文件
    <input type="file" name="" id="">
</a>

CSS样式1:

/*a  upload */
.a-upload {
    padding: 4px 10px;
    height: 20px;
    line-height: 20px;
    position: relative;
    cursor: pointer;
    color: #888;
    background: #fafafa;
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
    display: inline-block;
    *display: inline;
    *zoom: 1
}

.a-upload  input {
    position: absolute;
    font-size: 100px;
    right: 0;
    top: 0;
    opacity: 0;
    filter: alpha(opacity=0);
    cursor: pointer
}

.a-upload:hover {
    color: #444;
    background: #eee;
    border-color: #ccc;
    text-decoration: none
}

样式2:

.file {
    position: relative;
    display: inline-block;
    background: #D0EEFF;
    border: 1px solid #99D3F5;
    border-radius: 4px;
    padding: 4px 12px;
    overflow: hidden;
    color: #1E88C7;
    text-decoration: none;
    text-indent: 0;
    line-height: 20px;
}
.file input {
    position: absolute;
    font-size: 100px;
    right: 0;
    top: 0;
    opacity: 0;
}
.file:hover {
    background: #AADFFD;
    border-color: #78C3F3;
    color: #004974;
    text-decoration: none;
}

from 

https://www.haorooms.com/post/input_file_leixing

http://www.haorooms.com/post/css_input_uploadmh

备注:对于HTML5之input:file,还可以控制上传的类型的,但是这个是html5的,低版本浏览器不支持,详情请看:http://www.haorooms.com/post/input_file_leixing (HTML5的input:file上传类型控制)


No comments:

Post a Comment