I have added a file upload to my asp.net website. However, I want to limit the file types that user can select. For example, I only the user to select mp3 files. How can I add a filter to the file upload so that it displays only the mp3 files in the folder selected?
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnAudUpload" Text="Upload" CssClass="btncssUpload" OnClick="btnAudUpload_Click" runat="server" />
There are no options for the default file uploader, but you can use tools such as Uploadify to fulfill this goal. However, it is flash based if that is a problem. You can try it out on their limited file types demo.
If you do not want to use flash, it would be easiest to do the validation yourself via javascript or on the server side and inform the user if the file's type is not valid.
file-input-accept-attribute-is-it-useful is another similar question that may have some useful information.
As stated above, it's not possible out of the box.
The simpler solution that I've found: use a RegularExpressionValidator to check the file extension. No need for JavaScript or external libraries. Of course, it only checks the extension, not the file content (you must use server-side code and inspect the bytes), and does not change anything to the file list displayed in the folder browser.
I have a similar application that is being used to upload PDF files. While it would be great if the Upload Control had a file type filter out of the box, I found it wouldn't really solve the problem of limiting the file type to upload.
For instance, if a user were to simply rename a Word document from "myfile.docx" to "myfile.pdf" the system would assume it was a valid file, even though the actual file encoding is invalid; this would cause issues in other parts of the application.
To actually solve the issue, you can take the byte array from the control and parse it as a string. Then apply a filter. Here is the code I have:
Of course you will need to know what patterns are valid for your file type, and may want to use a RegEx instead of the .Net string helper method, but the general idea is to actually check the actual file contents and not rely on the file extension for validation.
Ryan A.
Use the following code js code for to only select the required file type which we want to select. IN the below example I want to select only zip file, On browse it only shows the zip file extension file name
Use the accept attribute directly in the tag (it's not really supported by the control, but will be delivered to client anyway)
While you could list file extensions, e.g: ".xls,.xlsx", this is NOT recommended, and some browsers get confused by it.
It's better to use MIME types (browser will map them to appropriate extensions for you):
Use comma-separated list if needed, e.g.:
Supported browsers and more info: http://www.w3schools.com/tags/att_input_accept.asp
Common MIME types below (snapshot of http://www.sitepoint.com/web-foundations/mime-types-summary-list/ )