HTML 输入 =“文件"接受属性文件类型 (CSV) [英] HTML Input="file" Accept Attribute File Type (CSV)

查看:39
本文介绍了HTML 输入 =“文件"接受属性文件类型 (CSV)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个文件上传对象:

在我的桌面上有以下 excel 文件:

<块引用>

  1. file1.xlsx
  2. file1.xls
  3. 文件.csv

我希望文件上传到 ONLY 显示 .xlsx.xls、&.csv 文件.

使用 accept 属性,我发现这些内容类型处理了 .xlsx &.xls 扩展名...

<块引用>

accept= application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (.XLSX)

accept= application/vnd.ms-excel (.XLS)

但是,我找不到 Excel CSV 文件的正确内容类型!有什么建议吗?

示例:http://jsfiddle.net/LzLcZ/

解决方案

嗯,这很尴尬...我找到了我正在寻找的解决方案,而且再简单不过了.我使用以下代码来获得所需的结果.

有效接受类型:

对于 CSV 文件 (.csv),请使用:

对于 Excel 文件 97-2003 (.xls),使用:

对于 Excel Files 2007+ (.xlsx),使用:

对于文本文件 (.txt) 使用:

对于图像文件(.png/.jpg/etc),使用:

对于HTML 文件(.htm、.html),使用:

对于视频文件(.avi、.mpg、.mpeg、.mp4),使用:

对于音频文件(.mp3、.wav 等),请使用:

对于PDF 文件,使用:

演示:
http://jsfiddle.net/dirtyd77/LzLcZ/144/


注意:

如果您尝试显示 Excel CSV 文件 (.csv),请不要使用:

  • text/csv
  • application/csv
  • text/comma-separated-values(仅适用于 Opera ).

如果您尝试显示特定文件类型(例如,WAVPDF),那么这几乎总是有效...

 

I have a file upload object on my page:

<input type="file" ID="fileSelect" />

with the following excel files on my desktop:

  1. file1.xlsx
  2. file1.xls
  3. file.csv

I want the file upload to ONLY show .xlsx, .xls, & .csv files.

Using the accept attribute, I found these content-types took care of .xlsx & .xls extensions...

accept= application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (.XLSX)

accept= application/vnd.ms-excel (.XLS)

However, I cannot find the correct content-type for an Excel CSV file! Any suggestions?

EXAMPLE: http://jsfiddle.net/LzLcZ/

解决方案

Well this is embarrassing... I found the solution I was looking for and it couldn't be simpler. I used the following code to get the desired result.

<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />  

Valid Accept Types:

For CSV files (.csv), use:

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

For Excel Files 97-2003 (.xls), use:

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

For Excel Files 2007+ (.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" />

这篇关于HTML 输入 =“文件"接受属性文件类型 (CSV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆