About CSV files
Registered in RFC 4180, which also describes the format. For exports, send text/csv; charset=utf-8 with Content-Disposition: attachment; filename="export.csv". Excel ignores the charset header and guesses the encoding itself, so add a UTF-8 byte order mark (EF BB BF) at the start of the file if non-English text must open correctly in Excel.
-
In upload forms, accept
.csvby extension too: Windows browsers often report CSV files asapplication/vnd.ms-excel, and some asapplication/octet-stream.
Other types seen for these files:
application/vnd.ms-excel
,
application/csv
,
text/x-csv
. Accept them when reading uploads, but send text/csv yourself.
File signature
CSV has no fixed signature: it is text without a magic number, so validate uploads by parsing the content rather than by checking bytes.
Serve .csv with the right Content-Type
Content-Type: text/csv; charset=utf-8
text/csv csv;
AddType text/csv .csv
AddCharset UTF-8 .csv
@csv path *.csv
header @csv Content-Type "text/csv; charset=utf-8"
curl -sI https://example.com/file.csv | grep -i '^content-type'
# Content-Type: text/csv; charset=utf-8
Set the type in code
res.set('Content-Type', 'text/csv; charset=utf-8');
import mimetypes
mimetypes.add_type('text/csv', '.csv') # if your system doesn't know it
mimetypes.guess_type('file.csv')[0] # 'text/csv'
mime.AddExtensionType(".csv", "text/csv; charset=utf-8")
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
Accept .csv uploads
<input type="file" accept=".csv,text/csv">
The accept attribute only filters the file picker; users can still choose another file. Listing the
extension as well as the type matters, because operating systems don’t always report the type you expect.
Related types
-
.tsv (
text/tab-separated-values): TSV -
.xlsx (
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet): Excel workbook -
.json (
application/json): JSON
More data and text
.txt , .md , .ndjson , .jsonld , .xml , .yaml , .geojson , .ics , .sql . Look up any extension or type with the MIME type lookup.
Sources
Spotted a mistake? Email hello@devtoolsmith.com and we’ll fix it.
Frequently asked questions
What is the MIME type for .csv files?
text/csv. You may also see application/vnd.ms-excel, application/csv, text/x-csv, which are older or unofficial; send text/csv. It is a text format, so add the character set: "Content-Type: text/csv; charset=utf-8".
Why does my server send the wrong Content-Type for .csv?
Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .csv is missing from that table, they send a default such as application/octet-stream or text/plain. Add the mapping as shown on this page, then check the response with "curl -sI".
How can I check that a file really is CSV?
CSV has no fixed file signature, so the extension and content are all you have. Check that the content is what you expect, for example by parsing it, before trusting an upload.