File Download - Restricting file types in the File Download window

I have a question about downloading files, I need to limit the types of files, but I like to do this in the "Upload files" window, so the user cannot even see files that are not allowed for downloading, I like to do this in js or jquery.

I know that this is possible, for example, adds a plugin, but I do not want to use it for a number of reasons.

+4
source share
2 answers

Try this link. He should give you what you need. It just uses direct javascript, I don’t think there are any jQuery options for it yet.

http://www.codestore.net/store.nsf/unid/DOMM-4Q8H9E

<script type="text/JavaScript"> <!-- Begin function TestFileType( fileName, fileTypes ) { if (!fileName) return; dots = fileName.split(".") //get the part AFTER the LAST period. fileType = "." + dots[dots.length-1]; return (fileTypes.join(".").indexOf(fileType) != -1) ? alert('That file is OK!') : alert("Please only upload files that end in types: \n\n" + (fileTypes.join(" .")) + "\n\nPlease select a new file and try again."); } // --> </script> 

I updated this answer with the actual code from the published link. As the poster notes, this is not a β€œsafe” solution to this problem. Since this is client-side javascript, the file extension can be easily changed using web development tools. Always check downloaded files on the server side to prevent malicious files from downloading.

+2
source

As I know, we can do this, as shown below, but still the dialog contains all file types:

 <input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" /> 

But you can limit the file upload to the wrong type using Jquery / Javascript, I think, before sending the file to the server, checking the extension.

0
source

All Articles