DevToolsmith

SVG MIME Type

image/svg+xml is the MIME type for SVG vector images. Here is the Content-Type header to send, the file signature, and how to configure servers and code.

MIME type
image/svg+xml
Extensions
.svg, .svgz
Content-Type header
image/svg+xml; charset=utf-8
Registered
Yes, with IANA
Compress with gzip
Yes, it is text or uncompressed
Category
Web

About SVG image files

SVG is XML text, so it compresses very well. In <img> tags, scripts inside an SVG never run, but an SVG opened directly or embedded with <object> or <iframe> can run JavaScript. That makes user-uploaded SVGs an XSS risk: serve them from a separate domain, with Content-Disposition: attachment, or with Content-Security-Policy: script-src 'none'.

  • Pre-compressed .svgz files need Content-Encoding: gzip in addition to image/svg+xml, or browsers show an error.

File signature

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

Serve .svg with the right Content-Type

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

Set the type in code

Node.js (Express)
res.set('Content-Type', 'image/svg+xml; charset=utf-8');
Python
import mimetypes

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

Accept .svg uploads

HTML
<input type="file" accept=".svg,.svgz,image/svg+xml">

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

  • .png (image/png): PNG image
  • .webp (image/webp): WebP image
  • .html (text/html): HTML
  • .xml (application/xml): XML

More web

.css , .js , .mjs , .wasm , .webmanifest , .rss , .vtt . 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 .svg files?

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

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

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

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