input fileapi

1、input type='file'

需求:用户点击按钮弹出选择文件框框,可以多选,可以单选,然后将用户选择的文件缓存,需要在界面显示用户选择了哪些文件, 文件大小不能超过50M,不可以一次上传相同文件两次,上传之前可以逐一删除选择过的文件。

效果如下:

image

1、文件多选:给input加上 multiple="multiple"即可多选。

2、限制文件后缀:加上accept='.csv' accept的值就是你想控制的文件后缀名。

3、样式:input file的样式不可改变,将input透明,用div做一个按钮,将input覆盖上面即可。

4、上传文件:项目框架react,UI层用ant design,故第一次开发时候选用了Upload组件,该组件上传一个文件很方便,直接引入组件即可,但是组件有一个缺点,点击就会直接上传,与需求不符。想来想去决定用户选择文件即把文件传给后端,后端将该文件对应的路径返回给我,在用户点击导入的时候,将对应的用户信息和文件路径在给后端传过去。但是由于项目进展,后端共享服务器,用户每次上传的文件会散落在多台服务器上,所以两次上传pass,决定用原生来做。
解决方案链接

其他:
html input="file" 浏览时只显示指定文件类型

Valid Accept Types:

For

CSV

files (.csv), use:

<input type="file" accept=".csv" />

For

Excel Files 2003-2007

(.xls), use:

<input type="file" accept="application/vnd.ms-excel" />

For

Excel Files 2010

(.xlsx), use:

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

For

Text Files

(.txt) use:

<input type="file" accept="text/plain" />

For

Image Files

(.png/.jpg/etc), use:

<input type="file" accept="image/*" />

For

HTML Files

(.htm,.html), use:

<input type="file" accept="text/html" />

For

Video Files

(.avi, .mpg, .mpeg, .mp4), use:

<input type="file" accept="video/*" />

For

Audio Files

(.mp3, .wav, etc), use:

<input type="file" accept="audio/*" />

For

PDF Files

, use:

<input type="file" accept=".pdf" /> 

DEMO:
http://jsfiddle.net/dirtyd77/LzLcZ/144/


NOTE:

If you are trying to display Excel CSV files (.csv), do

NOT

use:

  • text/csv

  • application/csv

  • text/comma-separated-values (

    works in Opera only

    ).


If you are trying to display a

particular file type

(for example, a WAV or PDF), then this will almost always work...

 <input type="file" accept=".FILETYPE" />

原文:http://stackoverflow.com/questions/11832930/html-input-file-accept-attribute-file-type-csv

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 1、没时间、没精力 这个很好理解,大家要上班,还要养家、想买房、想换车、想升职等各种事情往往已经把我们的所有时间和...
    饶新v阅读 978评论 0 1
  • 画这幅画,看了一整部老电影——理察·基尔和朱莉娅·罗伯茨主演的《Pretty Woman》(风月俏佳人)。那是罗伯...
    花开兮缓归阅读 1,770评论 0 4
  • 耳朵旁放着数码暴龙的音乐,有点燃,想起了中午李笑来老师的 与时间做朋友 中的一句话,是不是原话我忘了,大概意思是,...
    bluewind1230阅读 189评论 0 1
  • 近日有人问分手之后,前任生日是否应该说一句“生日快乐”。他觉得不方便打扰。 在我看来,一声问候很平常,并没有打扰与...
    化浊阅读 254评论 0 0