How to filter files that can be seen in the download window?

In an ASP.NET MVC application that should not have client-side objects, such as ActiveX, Flash, or Java applets (JavaScript is fine), is it possible to assume that only the files that I specify will appear when the file download dialog box appears ?

For example, only the .docx or docx and jpg extension files will be visible and can be selected in the open file dialog box to select the file to upload.

I read that there is a problem with browser support for this function, although this is what should work with the correct settings?

If I could get some examples, and some heads-up on that, it would be great.

Will AjaxControlKit be the one that supports this functionality?

Thanks,

Ric

0
source share
2 answers

Unable to filter files in the file download dialog. It depends on the browser, and no browsers provide this functionality.

However, once a file has been selected, its value can be checked using JavaScript. You can handle the event of sending a form element and match the input value of the file with the regular expression. Here are some unverified code examples:

<script type="text/javascript">
  function check(event)
  {
    if (!document.getElementById('file').value.match(/.*\.jpg/))
    {
      alert('File must have .jpg extension. Please try again.');
      return false;
    }
    return true;
  }
</script>

<form action="page.html" onsubmit="check">
  <input type="file" name="file" id="file"/>
</form>
+2
source

The input element supports the accept attribute, which is supported by modern browsers such as Chrome:

<input id="file" type="file" name="file" size="30" 
accept="image/jpg,image/png,image/jpeg,image/gif">

Unfortunately, this is not supported by IE, even IE9.

Sincerely, Martin Cordoba www.martincordova.com Dinamica - Java EE / Ajax / SQL with Webapp generators based on Eclipse.

+2

Source: https://habr.com/ru/post/1720526/


All Articles