【发布时间】:2015-02-12 20:36:43
【问题描述】:
我目前正在尝试使用 C# 在 Visual Studio、ASP.net 中构建一个小型网站。 用户必须上传文件,但我只想接受 Excel 文件。
目前我已经有了这个服务器端验证:
public ActionResult Index(HttpPostedFileBase file){
if (file != null && file.ContentLength > 0){
string fileName = Path.GetFileName(file.FileName);
if (fileName.EndsWith(".xlsx")) //enkel gewoon Excel bestand wordt aanvaard. {
...
这可以解决问题,但我不知道如何在客户端仅允许 Excel 文件。我目前有这个,但它还没有限制用户。我仍然可以上传任何我想要的文件。
<input type="file" name="file" value="Bestand kiezen" accept=".xlsx" />
我知道客户端限制并非 100% 万无一失,但我还是想实现该功能。提前致谢!
编辑:感谢下面的家伙,我得到了答案。我最终使用了这个,它显示了 .xls 和 .xlsx 文件
<input type="file" name="file" value="Bestand kiezen" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
【问题讨论】:
-
这可能有用:stackoverflow.com/a/11834872/1870760 检查 excel 案例的接受属性
标签: c# html asp.net validation client-side