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 can anyone guide me on this issue. Thanks.
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...
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;
}