上传前验证文件扩展名

时间:2015-06-10 05:12:50

标签: javascript asp.net

我想在上传之前验证文件扩展名,所以我创建了fileUpload控件,如下所示

<asp:FileUpload ID="FileUpload1" runat="server" OnChange="return validateFileExtension(this)"/> 

并创建java脚本以在上传之前验证文件的扩展名

<script lang="javascript" type="text/javascript">
var validFileExtensions = [".txt", ".TXT"];
function ValidateFileUpload(Source, args) {
var fuData = document.getElementById('<%= FileUpload1.ClientID%>');
var FileUploadPath = fuData.value;
if (FileUploadPath == '') {
// There is no file selected 
args.IsValid = false;
}
else {
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "txt" || Extension == "TXT") {
args.IsValid = true; // Valid file type
FileUploadPath == '';
 }
 else {
 alert("Please upload only text file")
 }
 }
 }
</script> 

我得到0x800a1391 - Javascript运行时错误:'validateFileExtension'未定义

1 个答案:

答案 0 :(得分:0)

<asp:FileUpload ID="FileUpload1" runat="server" OnChange="return ValidateFileUpload(this)"/>

ValidateFileUpload代替validateFileExtension