Is there any way to show only specific file types in the dropdown menu of the file upload window, as shown below, through ASP.NET? For example I only want to allow *.xlsx files to be selected from the explorer. I might have seen this done in some applications, but can't find a way to implement it.
Asked
Active
Viewed 3,342 times
0
-
1Take a look at http://stackoverflow.com/questions/4328947/limit-file-format-when-using-input-type-file for example – mortb Nov 07 '12 at 08:33
-
Is there an ASP.NET specific solution to this? They are doing it through Javascript. – CobaltBabyBear Nov 07 '12 at 08:38
2 Answers
2
For excel you can try:
<input type="file" name="myFile" accept="application/vnd.ms-excel" />
if you want to use the asp.net fileupdload control should be able to write
<asp:FileUpload id="someId" accept="application/vnd.ms-excel" runat="server" />
but you can't rely on it cross browser. You still need to validate the file in the code behind. The easiest way (but by no means comprehensive) to verify the file in the code behind is to check the extension by calling the method Path.GetFileExtension(fileName)
if is the wrong extension you'll return an error to the user. I'd usually put this check in the "submitbutton_click" event.

mortb
- 9,361
- 3
- 26
- 44
-
Thanks. It's not working in IE though. I'll have to do some extra work for that. Cheers! – CobaltBabyBear Nov 07 '12 at 10:43
1
Its not possible to restrict the file type out of the box, however you can use a RegularExpressionValidator to check the file extension:
<asp:RegularExpressionValidator ControlToValidate="FUpload1" ValidationExpression="^.*\.(jpg|JPG)$" runat="server" />

Jon Susiak
- 4,948
- 1
- 30
- 38
-
I did that before but my PM got a bit grouchy about it. That's not what is required. – CobaltBabyBear Nov 07 '12 at 08:41