RegularExpressionValidator to validate PDF file in ASP.Net

rakeshkuma
 
on Jan 01, 2021 11:51 PM
Sample_106964.zip
1452 Views

When user try to upload pdf file, file name as "Terms & Policy.pdf" as per RegularExpressionValidator it is not accepting.

<asp:FileUpload ID="gv_fudoc" runat="server" class="upload up" accept=".pdf" />
<asp:RegularExpressionValidator ID="regpdf" Display="Dynamic" ForeColor="#ff3636" SetFocusOnError="true" Font-Size="11px" Style="letter-spacing: 1px" ErrorMessage="Please upload only PDF file" ControlToValidate="gv_fudoc" ValidationGroup="gv_rfdoc" ValidationExpression="^.*\.(pdf|PDF)$" runat="server">             <i class="fa fa-exclamation-triangle"></i>&nbsp;  Please upload only pdf file. </asp:RegularExpressionValidator>

Try to upload pdf file name as "Terms & Policy.pdf" using following code. It will not accept. 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 02, 2021 04:47 AM

I have checked the code. Its working.

Refer below code.

HTML

<asp:FileUpload ID="gv_fudoc" runat="server" class="upload up" accept=".pdf" />
<asp:RegularExpressionValidator ID="regpdf" ErrorMessage="Please upload only PDF file"
    ControlToValidate="gv_fudoc" ValidationExpression="^.*\.(pdf|PDF)$" runat="server"
    Display="Dynamic" ForeColor="#ff3636" />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />

C#

protected void OnSave(object sender, EventArgs e)
{
    gv_fudoc.SaveAs(Server.MapPath("~/") + Path.GetFileName(gv_fudoc.PostedFile.FileName));
}