Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对文件输入执行客户端验证-接受属性无效_C#_Html_Asp.net_Validation_Client Side - Fatal编程技术网

C# 对文件输入执行客户端验证-接受属性无效

C# 对文件输入执行客户端验证-接受属性无效,c#,html,asp.net,validation,client-side,C#,Html,Asp.net,Validation,Client Side,我目前正试图在VisualStudio中用C构建一个小网站ASP.net。 用户必须上传文件,但我只想接受Excel文件 目前,我已经完成了以下服务器端验证: public ActionResult Index(HttpPostedFileBase file){ if (file != null && file.ContentLength > 0){ string fileName = Path.GetFileName(file.FileName);

我目前正试图在VisualStudio中用C构建一个小网站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" />
accept属性受某些浏览器支持,但并非所有浏览器都支持,并且支持级别各不相同;例如,参见和的答案

请注意,在支持浏览器的情况下,accept=.xlsx会限制对文件扩展名为.xlsx的文件的输入,因此通常会忽略使用旧版本Excel创建的Excel文件,这些文件会生成.xls文件。通常,任何可以处理.xlsx的软件都可以处理.xls。


它应该可以工作

这可能很有用:检查excel casesMmhmm上的accept属性,这样就可以了,是的。弹出窗口仍然使用标准。文件,但它也可以选择使用我指定的类型。谢谢,顺便说一句!