How can i upload only jpeg files?

10,164

Solution 1

You could use a RegularExpressionValidator to validate if the user tries to upload 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 serverside:

    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.";
        }
    }

You should know that any user could change the extension f.e. from .exe to .jpg. The only way i know to check for the real file-type would be to use function from Urlmon.dll. Have a look at this SO-question if you want further informations: Using .NET, how can you find the mime type of a file based on the file signature not the extension

Solution 2

Those will probably help you.

Share:
10,164
jakobiyem
Author by

jakobiyem

Updated on June 27, 2022

Comments

  • jakobiyem
    jakobiyem almost 2 years

    i want to upload files that are only jpeg, jpg etc. But i couldn't filter the files in the opening window. I want to change the text "all files" to jpeg etc. in the asp.net. (C#)

    • mingos
      mingos over 13 years
      I believe it should be possible to check the uploaded files' mime type. It is possible with PHP, so it would be surprising it it wasn't possible with asp.net. As for HOW you can do that - sorry, I'm not the one to provide an answer :(.
    • adrianbanks
      adrianbanks over 13 years
      However you do the server side validation, you might want to also consider checking that the file actually contains a JPG (ie. it isn't just another file type with the .jpg extension).
  • Brad Christie
    Brad Christie over 13 years
    It's possible, just requires javascript/workaround. But Ultimately, you're right, you can't confirm it until after it's already been uploaded. And just to note, it's still a good idea to validate server-side to confirm the file's only an image (for malformed POST protection purposes)
  • Viacheslav Smityukh
    Viacheslav Smityukh over 13 years
    You are right, you can check selected file, but you can't set files filter to the file select dialog.
  • Brad Christie
    Brad Christie over 13 years
    +1 for both server and client-side validation, in native ASP.
  • Filip Ekberg
    Filip Ekberg over 13 years
    You should rename those controls / methods. :)
  • Tim Schmelter
    Tim Schmelter over 13 years
    It's only an example, the OP didn't offer any markup, so i used this "anonymous" controls ;-)