0

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.

SnapShot

CobaltBabyBear
  • 2,188
  • 6
  • 26
  • 47

2 Answers2

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
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