Filter File extension With FileUpload

2019-04-05 18:05发布

问题:

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.

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.

回答1:

I believe it is not possible.Filter by file type in browser is not control by asp.net.It is a browser functionality.

This question already asked in stackoverflow.Please click filter the file type with the file upload control



回答2:

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.



回答3:

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


回答4:

you can create validate file extension from the ASP.NET FileUpload Control

I believe you could go for this solution

Validate File Extension for ASP.NET FileUpload Control



回答5:

It's possible on client-side! But, remember to do the server-side validation.

This two links show it: http://aspalliance.com/1614_Adding_Filter_Action_to_FileUpload_Control_of_ASPNET_20.all http://www.codeshode.com/2011/07/validate-file-extension-for-aspnet.html