3

i am using file upload, i wanted to restrict the files showing in the dialog box to images only. That is 'Files of Type' in the dialog box should be .jpg,.jpeg,.gif,.bmp,.png

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yajuvendra Vant
  • 1,127
  • 4
  • 15
  • 33

5 Answers5

2

You can't. Web browsers don't allow you to do things like filter the list by filetype or set the default directory for the file upload dialog.

As Darin and Chris have suggested, once the user has selected a file you can use javascript to parse the filename and alert the user if it doesn't look like the file is of the right type. Depending on what you are going to do with the file you should consider do something on the server side to verify that the file is valid image and not something bad .

Alternatively, you could look into using Silverlight's OpenFileDialog or maybe even a Flash control. See http://www.plupload.com, http://www.uploadify.com/, http://swfupload.org/ etc...

Community
  • 1
  • 1
nick
  • 3,368
  • 5
  • 23
  • 26
0

I know this is very old but With Fileuploads in asp. If I want to lock to content to be a certain type like image or video I just do a contains on the content type.

if (FileUpload1.HasFile) {

    if (FileUpload1.PostedFile.ContentType.Contains("image/")) {
//rest of your logic
    }

}
Malcor
  • 2,667
  • 21
  • 29
0

You could use a regular expression validator.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

You'll want to do this two ways: one on the client for ease of use, and then once on the server to protect against users disabling the client side validation. Both approaches are described here.

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
-1

this function is used to check whether the file that user want to upload is valid file type or not.

private bool IsValidFile(string filePath)
    {
        bool isValid = false;

        string[] fileExtensions = { ".BMP", ".JPG", ".PNG", ".GIF", ".JPEG" };

        for (int i = 0; i < fileExtensions.Length; i++)
        {
            if (filePath.ToUpper().Contains(fileExtensions[i]))
            {
                isValid = true; break;
            }
        }
        return isValid;
    }

This function used to check file type & file size. If file is invalid than it will return error message.

private string ValidateImage(HttpPostedFile myFile)
   {
       string msg = null;
       int FileMaxSize = Convert.ToInt32(ConfigurationManager.AppSettings["FileUploadSizeLimit"].ToString());
       //Check Length of File is Valid or Not.
       if (myFile.ContentLength > FileMaxSize)
       {
           msg = msg + "File Size is Too Large.";
       }
       //Check File Type is Valid or Not.
       if (!IsValidFile(myFile.FileName))
       {
           msg = msg + "Invalid File Type.";
       }
       return msg;
   }
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • this will not run before the files have been uploaded, wasting the customers/users time. – BerggreenDK Feb 17 '12 at 10:48
  • can you explain it to me how? this is on server side checking and my code is running without any problem – Nick Kahn Feb 19 '12 at 01:21
  • this code checks AFTER the customer HAS waited for the upload to happen. It does not help the customer choose the file in the OpenFile dialog on the client in a browser. Lets say I want to upload a 20 Mb file, then I'll have to wait until 20 Mb is uploaded, before I get the actual error from your code. – BerggreenDK Feb 22 '12 at 23:12