12

I tried to filter file format to accept only .txt in HTML. Here's my HTML code:

<Input
 type="file"
 accept="text/plain"
/>

In Safari, it works and only .txt files show up, but in Chrome (63.0.3239.84) the file selector also shows .csv files.

Is it possible to exclude .csv files in Chrome?

3 Answers 3

14

The accept attribute specifies the types of files that the form input will accept.

Syntax

<input accept="file_extension|audio/*|video/*|image/*|media_type">

Tip: To specify more than one value, separate the values with a comma (e.g. .


Solution

Simply include the filetype you'd like to allow in the accept attribute, as follows:

<input accept=".txt"

Note: Because this file-restriction is client-side, users are able to remove this attribute and bypass this file-restriction, potentially leading to a vulnerability.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for response I tried the suggestion, but accept=".txt" will include '.csv' in Chrome, even include '.docx' in Safari
as I tried (on Mac), accept="text/plain" in Safari will get the result I expect (exclude .csv) and Chrome both accept '.txt', 'text/plain' will shows .csv file. I guess I can only give some placeholder or tooltip to user, anyway, thanks for your help!
This syntax still allow users to attach .js .py files, etc.
This doesn't appear to be working for Chrome anymore. I'm on 86.0.4240.183.
There seems to be a bug around this in macOS Chromium. See this issue, and this related question.
|
1

Its a chrome and mac os bug, Please refer and vote it. https://bugs.chromium.org/p/chromium/issues/detail?id=1353740

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

I have not tried it but I think it will work:

<input type="file" accept=".txt"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.