HTML输入="文件"接受属性文件类型(CSV)

Dom*_*Dom 453 html csv content-type file-upload input

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

<input type="file" ID="fileSelect" />
Run Code Online (Sandbox Code Playgroud)

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

  1. file1.xlsx
  2. file1.xls
  3. FILE.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/

Dom*_*Dom 1131

这是令人尴尬的...我找到了我正在寻找的解决方案,它不可能更简单.我使用以下代码来获得所需的结果.希望这可以帮助将来的某个人.谢谢大家的帮助.

<input id="fileSelect" type="file" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />  
Run Code Online (Sandbox Code Playgroud)

有效接受类型:

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

<input type="file" accept=".csv" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="application/vnd.ms-excel" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="text/plain" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="image/*" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="text/html" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="video/*" />
Run Code Online (Sandbox Code Playgroud)

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

<input type="file" accept="audio/*" />
Run Code Online (Sandbox Code Playgroud)

对于PDF文件,请使用:

<input type="file" accept=".pdf" /> 
Run Code Online (Sandbox Code Playgroud)

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


注意:

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

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

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

 <input type="file" accept=".FILETYPE" />
Run Code Online (Sandbox Code Playgroud)

  • @Vroo是的,在阅读[本文](http://w3fools.com/)后,我删除了对w3schools的所有引用. (20认同)
  • 这个答案有效.请注意,上面的w3schools链接有错误的答案.我首先找到了该页面,但它没有帮助; stackoverflow ftw. (8认同)
  • 但是,这在Safari中无效. (5认同)
  • `&lt;input type="file" accept=".csv, text/csv" /&gt;` 在 mac 上的 Firefox、Chrome 和歌剧中为我工作。只有 .csv 不能在所有浏览器中工作。 (4认同)
  • Chrome似乎支持此属性,但Firefox仍在使用它.您可以投票给这个错误,以便他们更快地解决它:https://bugzilla.mozilla.org/show_bug.cgi?id = 826176 (3认同)
  • @DavidRouten accept属性只会过滤文件类型.您还必须使用文件验证来阻止用户选择其他文件类型.希望有所帮助! (2认同)

Big*_*ney 131

这些天你可以使用文件扩展名

<input type="file" ID="fileSelect" accept=".xlsx, .xls, .csv"/>
Run Code Online (Sandbox Code Playgroud)

  • 尽管令人惊讶的是这个(以及替代的`application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel`)似乎仍然不适用于Edge 41.16299.820.0 https://stackoverflow.com/questions /31875617/input-accept-attribute-in-microsoft-edge#32214646 https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/13661175-full-spec-support-for-accept-输入类型文件 (3认同)

yog*_*ogi 38

Dom这个属性很老,据我所知在现代浏览器中不被接受,但是这里有一个替代它,试试这个

<script type="text/javascript" language="javascript">
function checkfile(sender) {
    var validExts = new Array(".xlsx", ".xls", ".csv");
    var fileExt = sender.value;
    fileExt = fileExt.substring(fileExt.lastIndexOf('.'));
    if (validExts.indexOf(fileExt) < 0) {
      alert("Invalid file selected, valid files are of " +
               validExts.toString() + " types.");
      return false;
    }
    else return true;
}
</script>

<input type="file" id="file" onchange="checkfile(this);" />
Run Code Online (Sandbox Code Playgroud)

我想它会帮助你,当然你可以根据你的需要改变这个脚本.

  • 很酷的解决方法,但我不明白为什么这样的属性被认为是'旧'.这是几乎所有操作系统的基本文件选择器功能,浏览器应该尽力使其工作,它将帮助许多用户...... (5认同)
  • Accept 属性并不旧,除了 IE/Edge 之外的主要浏览器都支持:http://caniuse.com/#feat=input-file-accept。请重新表述您的答案,因为它具有误导性。 (4认同)
  • `accept` 属性最重要的方面是它提供给打开文件对话框的提示。默认情况下,用户应该看到匹配的文件,如果他们愿意的话,大概可以选择其他类型——这是大多数现代浏览器现在的行为方式。 (2认同)

jay*_*red 12

text/comma-separated-values在accept属性中使用了CSV mime-type,它在Opera中运行正常.试了text/csv没运气.

如果建议不起作用,则其他一些用于CSV的MIME类型:

  • 文本/逗号分隔值
  • 文/ CSV
  • 应用程序/ CSV
  • 应用程序/ EXCEL
  • 应用/ vnd.ms-Excel中
  • 应用程序/ vnd.msexcel
  • 文/ anytext

资料来源:http://filext.com/file-extension/CSV

  • 只需使用`.csv`就像`<input type ="file"accept =".csv"/>` (2认同)
  • 嗨,多姆!我想说抱歉,因为你的答案(标记为正确)是好的,我之前没有太多关注它,因为我只在 Opera 中测试该网站。在其他浏览器中测试后,我发现您的答案更完整。但它并不适用于所有浏览器。Firefox 17 不支持在文件对话框中接受 attr 如何过滤(https://bugzilla.mozilla.org/show_bug.cgi?id=83749#c14),所以这个属性对我来说是个悬念。无论如何,我将使用 javascript 文件验证,但在接受 attr 中使用 text/csv,因为它是 IANA http://www.iana.org/assignments/media-types 的默认设置 (2认同)

tro*_*jan 9

这在Safari 10下对我不起作用:

<input type="file" accept=".csv" />
Run Code Online (Sandbox Code Playgroud)

我不得不写这个:

<input type="file" accept="text/csv" />
Run Code Online (Sandbox Code Playgroud)


小智 7

只需在 out 的操作 atr 中写下您想要接受的扩展名,以逗号分隔即可

<input type="file" accept=".any, .ext, .you, .want">
Run Code Online (Sandbox Code Playgroud)


小智 6

例如,除了最佳答案之外,CSV 文件在 macOS 下报告为 text/plain,但在 Windows 下报告为 application/vnd.ms-excel。所以我用这个:

<input type="file" accept="text/plain, .csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
Run Code Online (Sandbox Code Playgroud)


小智 5

您只需执行以下操作即可知道任何文件的正确内容类型:

1)选择感兴趣的文件,

2)并在控制台中运行:

console.log($('.file-input')[0].files[0].type);
Run Code Online (Sandbox Code Playgroud)

您还可以为输入设置属性“multiple”,以一次检查多个文件的内容类型,然后执行下一步:

for (var i = 0; i < $('.file-input')[0].files.length; i++){
    console.log($('.file-input')[0].files[i].type);
}
Run Code Online (Sandbox Code Playgroud)

属性接受对于多个属性存在一些问题,在这种情况下无法正常工作。


Lan*_*Yan 5

经过我的测试,在\xe3\x80\x90macOS 10.15.7 Catalina\xe3\x80\x91上,\xe3\x80\x90Dom / Rikin Patel\xe3\x80\x91的答案无法正常识别[.xlsx]文件。

\n

我个人总结了大部分现有答案的做法,并通过了亲身测试。总结一下以下几个答案:

\n
\n
accept=".csv, .xls, .xlsx, text/csv, application/csv,\ntext/comma-separated-values, application/csv, application/excel,\napplication/vnd.msexcel, text/anytext, application/vnd. ms-excel,\napplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet"\n
Run Code Online (Sandbox Code Playgroud)\n
\n