J888
J888

Reputation: 1964

How to restrict users to upload certain type of file?

I need to restrict users to upload certain type of files, how should I do that? I know that I can receive the file and check the ContentType variable but I need to change the way their browser looks for files.

Lets say the required file type is csv, when they click on upload and the window will be opened to look for files in their file system, they should only be able to see files with csv extension. In this way they are forced to upload CSV files as no other file is visible to be selected.

<s:form id="uploadCSV" method="POST" action="add"  enctype="multipart/form-data">
    <s:file name="csv" label="Upload File"/>
    <s:submit/>
</s:form>

Upvotes: 2

Views: 6394

Answers (2)

Chris
Chris

Reputation: 7184

The HTML5 standard specifies an accept attribute for input elements. It allows you to specify the MIME type and the extension and the standard recommends specifying both, so your code should look like this:

<input type="file" accept=".csv,text/csv"></input>

HTML4 does not allow specifying extensions, so you can only use the MIME type:

<input type="file" accept="text/csv"></input>

Upvotes: 1

KishoreK
KishoreK

Reputation: 962

Some browsers support "accept" attribute with the Input File control. If you are looking for all browser support, you may need to use any flash based upload plugin.

Other than that, as you said checking the ContentType is the only way of doing it.

Upvotes: 3

Related Questions