DevToolsmith

TXT MIME Type

text/plain is the MIME type for plain text files. Here is the Content-Type header to send, the file signature, and how to configure servers and code.

MIME type
text/plain
Extensions
.txt, .text, .log
Content-Type header
text/plain; charset=utf-8
Registered
Yes, with IANA
Compress with gzip
Yes, it is text or uncompressed
Category
Data and text

About Plain text files

Always add a charset: text/plain; charset=utf-8. Without it, browsers guess the encoding, and accented letters or emoji turn into mojibake like é. text/plain is also what browsers fall back to for unknown text, and the safest type for showing user-provided text, because it is never rendered as HTML.

File signature

TXT has no fixed signature: it is text without a magic number, so validate uploads by parsing the content rather than by checking bytes.

Serve .txt with the right Content-Type

Response header
Content-Type: text/plain; charset=utf-8
nginx (a line in the types { } block of mime.types)
text/plain txt text log;
Apache (.htaccess or server config)
AddType text/plain .txt .text .log
AddCharset UTF-8 .txt .text .log
Caddy
@txt path *.txt *.text *.log
header @txt Content-Type "text/plain; charset=utf-8"
Check what a server sends
curl -sI https://example.com/file.txt | grep -i '^content-type'
# Content-Type: text/plain; charset=utf-8

Set the type in code

Node.js (Express)
res.set('Content-Type', 'text/plain; charset=utf-8');
Python
import mimetypes

mimetypes.add_type('text/plain', '.txt')  # if your system doesn't know it
mimetypes.guess_type('file.txt')[0]  # 'text/plain'
Go
mime.AddExtensionType(".txt", "text/plain; charset=utf-8")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")

Accept .txt uploads

HTML
<input type="file" accept=".txt,.text,.log,text/plain">

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

  • .csv (text/csv): CSV
  • .md (text/markdown): Markdown
  • .json (application/json): JSON

More data and text

.tsv , .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 .txt files?

text/plain. It is a text format, so add the character set: "Content-Type: text/plain; charset=utf-8".

Why does my server send the wrong Content-Type for .txt?

Web servers pick the type from the file extension using a table (mime.types in nginx and Apache). If .txt 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 TXT?

TXT 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.