0

I'm currently trying to build a small website in Visual Studio, ASP.net with C#. The user has to upload a file, but I only want to accept Excel files.

Currently I already have this piece of server side validation:

public ActionResult Index(HttpPostedFileBase file){
  if (file != null && file.ContentLength > 0){
      string fileName = Path.GetFileName(file.FileName);
      if (fileName.EndsWith(".xlsx")) //enkel gewoon Excel bestand wordt aanvaard. {
           ...

That does the trick, but I don't know how to solely allow Excel files in the client side. I currently have this, but it doesn't limit the user yet. I can still upload whatever file I want.

<input type="file" name="file" value="Bestand kiezen" accept=".xlsx" /> 

I'm aware that client side limitation isn't 100% fool proof, but I'd like to implement the feature anyway. Thanks in advance!

EDIT: I got the answer thanks to the fellas below. I ended up using this, which shows .xls and .xlsx files

<input type="file" name="file" value="Bestand kiezen" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />

2 Answers2

0

The accept attribute is supported by some browsers but not all, and the level of support varies; see e.g. answers to File input 'accept' attribute - is it useful? and Input accept attribute in Firefox

Note that accept=".xlsx" restricts, on supporting browser, the input to files that have a filename extensions of .xlsx, thereby usually ignoring Excel files created with older versions of Excel, which produce .xls files. Generally, any software that can handle .xlsx can handle .xls as well.

Community
  • 1
  • 1
Jukka K. Korpela
  • 178,198
  • 33
  • 241
  • 350
  • Mmhmm, that sort of worked, yes. The pop up window still uses standard *.* files, but it also has the option to use my specified types. thanks btw! – I_Am_Not_user3185054 Dec 14 '14 at 18:51
0

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> it should work

Alex Sikilinda
  • 2,790
  • 14
  • 31