1

我如何验证文件类型???。以下错误抛出异常“theFileElement is not found”

@using (Html.BeginForm("Index", "Data", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <input type="file" name="FileUpload" />
    <input type="submit" name="Submit" id="btnSubmit" value="Upload"/>

}


<script type="text/javascript">
  $('#btnSubmit').click(function (e){

            filename = theFileElement.value;
              if (!/\.csv$/i.test(filename)) {
                alert("Please upload csv file only");
                return false;
              }
              return true;

        });

</script>
4

1 回答 1

1

theFileElement 没有在任何地方定义。添加

var theFileElement = document.getElementById("FileUpload");

filename = theFileElement.value;

还要编辑你的文件输入看起来像

<input type="file" name="FileUpload" id="FileUpload" />
于 2012-12-04T01:51:54.610 回答