How can I upload only jpeg files?

I want to upload files that are only jpeg, jpg, etc. But I could not filter the files in the window that opens. I want to change the text "all files" to jpeg etc. At asp.net. (WITH#)

+1
source share
3 answers

You can use RegularExpressionValidator to check if the user is trying to load jpeg files or not:

<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
 Text="Upload File" />&nbsp;<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<asp:RegularExpressionValidator 
 id="RegularExpressionValidator1" runat="server" 
 ErrorMessage="Only jpeg files are allowed!" 
 ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
    +(.jpg|.JPG|.jpeg|.JPEG)$" 
 ControlToValidate="FileUpload1"></asp:RegularExpressionValidator>
<br />
<asp:RequiredFieldValidator 
 id="RequiredFieldValidator1" runat="server" 
 ErrorMessage="This is a required field!" 
 ControlToValidate="FileUpload1"></asp:RequiredFieldValidator>

on server:

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fileExt = 
               System.IO.Path.GetExtension(FileUpload1.FileName);

            if (fileExt == ".jpeg" || fileExt == ".jpg")
            {
                //do what you want with this file
            }
            else
            {
                Label1.Text = "Only .jpeg files allowed!";
            }
        }
        else
        {
            Label1.Text = "You have not specified a file.";
        }
    }

, f.e. .exe .jpg. - Urlmon.dll. SO-, : .NET, mime ,

+5
+1

HTML.

.

0

All Articles