javascript中针对特定文件类型的Fileupload控件验证.

问题描述:

先生,
我正在开发电子邮件应用程序,并且使用asp.net的文件上传控件,我想使用JavaScript代码进行验证以验证特定文件类型(例如.pdf和.docx)的文件上传控件,但是问题是警报触发了正确,但仍然提交了错误的文件..我想使用正确的javascript停止错误的文件提交.请帮忙.

sir,
I''m Developing an email application and m using a file upload control of asp.net i want to validate my file upload control for particular file type eg .pdf and .docx m using javascript code for validation but problem is that the alert fires properly but still it submits the wrong file..i want to stop wrong file submission using proper javascript. please help.

//1)
var id_value = document.getElementById("<%=FileUpload1.ClientID %>").value;
               if (id_value != '') {
                   var valid_extensions = /(.doc|.docx|.pdf)$/i;
                   if (valid_extensions.test(id_value)) {
                       alert('OK');
                   }
                   else {
                       alert('Invalid File')


                   }
               } 
               return true;
 }


//2)
System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName);
            mail.Attachments.Add(attachment);

            string ext = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (ext == ".doc" || ext == ".docx" || ext == ".pdf" || ext == ".DOC" || ext == ".DOCX" || ext == ".PDF")
            {

                smtpServer.Send(mail);
                


            }
            else
            {
                
                string Msg = "<script>alert('Please select .doc or .pdf files only');</script>";
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
                FileUpload1.Focus();
            }


这是m使用的两个代码.
在此先感谢


these are the two code m using..
thanks in advance

/i; 如果(valid_extensions.test(id_value)){ alert(' OK'); } 其他 { 警报(' 无效的文件') } } 返回 ; }
/i; if (valid_extensions.test(id_value)) { alert('OK'); } else { alert('Invalid File') } } return true; }


//2)
System.Net.Mail.Attachment attachment;
            attachment = new System.Net.Mail.Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName);
            mail.Attachments.Add(attachment);

            string ext = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (ext == ".doc" || ext == ".docx" || ext == ".pdf" || ext == ".DOC" || ext == ".DOCX" || ext == ".PDF")
            {

                smtpServer.Send(mail);
                


            }
            else
            {
                
                string Msg = "<script>alert('Please select .doc or .pdf files only');</script>";
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "alert", Msg, false);
                FileUpload1.Focus();
            }


这是m使用的两个代码.
在此先感谢


these are the two code m using..
thanks in advance


在下面尝试:
try Below:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function validate() {
var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID%>').value;
//Regular Expression for fileupload control.
var reg = /^(([a-zA-Z])|(\\{2}\w+)\


?)(\\(\ w [\ w].*))+(.doc | .docx | .DOC | .DOCX)
?)(\\(\w[\w].*))+(.doc|.docx|.DOC|.DOCX)