Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter File extension With FileUpload

I am writing an asp.net web app which involves the use of the FileUpload control.

Right now, this particular FileUpload control only expects .zip or .gz file types. If an incorrect type of file is uploaded, An error message is displayed to the user. This functionality is already implemented.

What I want to do is to filter the visible file types that the user sees when he clicks on "browse".

You may have seen a file opening dialog resembling the following.
enter image description here

I've circled the area that represents the file extension filter.

This is a feature so common that I expected it to be built in to the current FileUpload Control but after some searching online, I've found some posts that say it can't be done.

Those posts were from 2009, more than 2 years ago.

Now, My question is: does the current Asp.Net 4.0 support this feature?, and if it doesn't, do you know of any simple solution to get the functionality that I want.

I would like to again point out that I am able to validate whether or not the user chooses a supported file type. All I'm looking for is a cosmetic change, that will filter out unneeded file types in the open file dialog.

like image 389
Sam I am says Reinstate Monica Avatar asked Jan 23 '12 18:01

Sam I am says Reinstate Monica


People also ask

How do I restrict a file type in FileUpload control in asp net?

User could simply rename the extension of an unwanted file to something in the accept list. So you should approach the accept list in the file upload as a simple help to the user and at least validate on the back end.

How can check file in file upload control in asp net?

Usually, for checking the desired file extensions in a file upload control we use C# code to determine whether the file extension is valid or not. But, we can also validate the file type of the file upload control using the ASP.Net regular expression validation control.

What is importance of FileUpload control HasFile property?

The HasFile property gets a value indicating whether the FileUpload control contains a file to upload. Use this property to verify that a file to upload exists before performing operations on the file.


2 Answers

You can try the below code.

<asp:FileUpload ID="UploadFile" accept="image/*" multiple="false" runat="server" BorderStyle="None" />

It works with modern browsers.

Do not forget to validate the extensions using code behind procedures.

like image 94
Arun Banik Avatar answered Sep 23 '22 07:09

Arun Banik


To select images in fileupload control..

hope it will help you

asp:RegularExpressionValidator ID="rexp" runat="server" ControlToValidate="fupProduct"
     ErrorMessage="Only .gif, .jpg, .png, .tiff and .jpeg"
     ValidationExpression="(.*\.([Gg][Ii][Ff])|.*\.([Jj][Pp][Gg])|.*\.([Bb][Mm][Pp])|.*\.([pP][nN][gG])|.*\.([tT][iI][iI][fF])$)"></asp:RegularExpressionValidator
like image 21
saau Avatar answered Sep 21 '22 07:09

saau