0

I work with c# and asp.net

I created a webpage with a web form where you enter your information in order to submit it. There is also a file upload on my page: <asp:FileUpload ID="FileUploadPassfoto" runat="server"/> In my c# code behind i coded a IF-Loop which checks if something got uploaded. Like this:

if (FileUploadPassfoto.HasFile == true)
{
      HttpPostedFile file = FileUploadPassfoto.PostedFile;
      using (BinaryReader binaryReader = new BinaryReader(file.InputStream))
      {
          lehrling.passfoto = binaryReader.ReadBytes(file.ContentLength);
      }
      LabelPassfotoError.Visible = false;
}
else
{
     LabelPassfotoError.Visible = true;
     LabelError.Visible = true;
}

What it does is: As i said it checks if something got uploaded. If nothing got uploaded a ErrorLabel will be shown so the user knows he forgot to upload.

What i want to check too, is if the uploaded file is a image. To be more clear i only want to accept .jpg/.bmp and .gif. If a wrong format gets uploaded i want to display my ErrorLabel as well.

I dont really know how i should do this, can you please help me? Thank you

M.Y.Mnu
  • 718
  • 8
  • 22
  • Thank you, i know it is but i just wanted a anwer where my id's and methods..... etc. are shown so its easier for me... cause im new in this business xD i'll delete it. Thanks for you help @David – Alessandro Minneci Aug 05 '16 at 12:55
  • i just looked at the page you linked to me. i have no idea how i should implement this in my case! i'm sorry but i wont delete my question.... @David – Alessandro Minneci Aug 05 '16 at 12:57
  • What specifically is unclear? The more I look at that other answer, the more identical it seems to this question. Did you try to implement that function? Where did you get stuck? – David Aug 05 '16 at 13:04
  • @David how do i write this in a if-loop i tried this: if(myupload.postedfile.width......) i am trying to check if the image has the right resolution can you help me – Alessandro Minneci Aug 05 '16 at 13:22
  • For starters, an `if` isn't a "loop". And the function in the answer returns a `bool`, so you'd literally just check that function. `if (YourFunction(file))` Within that function you would check anything you need to check. The linked answer checks the MIME type, the file extension, if it can convert to a `Bitmap`, etc. Add any other checks you like. – David Aug 05 '16 at 13:25

2 Answers2

2
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string strFileWithoutExt = Path.GetFileNameWithoutExtension(strFileName);
        string strExtension = Path.GetExtension(strFileName);
        if (strExtension == ".jpg" || strExtension == ".bmp" || strExtension == ".gif")
        {
            string strImageFolder = "~/YourFilePath/";
            if (!Directory.Exists(Server.MapPath(strImageFolder)))
                Directory.CreateDirectory(Server.MapPath(strImageFolder));
            string _strPath = Server.MapPath(strImageFolder) + strFileName;
            FileUpload1.PostedFile.SaveAs(_strPath);
            Label1.Text = "Upload status: File uploaded.";
        }
        else
            Label1.Text = "Upload status: only .jpg,.bmp and .gif file are allowed!";
    }

Hope Its Help You much more....

M.Y.Mnu
  • 718
  • 8
  • 22
  • 1
    If you do this you should use `string strExtension = Path.GetExtension(strFileName.ToLower());` If someone uploads an image with a uppercase extension your validation will fail. – VDWWD Aug 05 '16 at 13:29
  • yes right, thanks :).. – M.Y.Mnu Aug 05 '16 at 13:32
  • can you help me get my asking right back. i got banned from asking cuz i didnt know how to ask and how to programm back then when i asked this. you would really help me with a upvote <3 – Alessandro Minneci Dec 13 '16 at 14:59
1

Here is a simplified version of the link that David has posted in the comments.

HttpPostedFile file = FileUploadPassfoto.PostedFile;
if (file.ContentType == "image/x-png" || file.ContentType == "image/pjpeg" || file.ContentType == "image/jpeg" || file.ContentType == "image/bmp" || file.ContentType == "image/png" || file.ContentType == "image/gif")
{
    // it is an image
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79