# File.ContentTypeOf — what a file is, read from its bytes

> The content type of a file, read from its bytes the way the platform's upload route reads one: a signature decides a binary type whatever the file is called, markup is HTML, SVG or XML whatever it is called, and only text that does not begin with "<" is typed by its name (.css, .js, .json, .md, …). Use it instead of the type a caller claimed.

<!-- id: storage-content-type-of · area: storage · stability: stable · html: https://osysharp.com/reference/storage/content-type-of/ -->

## Summary        {#summary}
An upload carries the type its sender CLAIMED — a browser reads it off the name, an agent writes whatever it likes. A
function that keeps a file, or serves it, should type it by what the bytes ARE:

```osy syntax
var type = File.ContentTypeOf(File.ReadAllBytes(upload.Path), upload.FileName);
```

## Signature      {#signature}
```osy syntax
string File.ContentTypeOf(byte[] bytes)                // by the bytes alone
string File.ContentTypeOf(byte[] bytes, string name)   // a name narrows text, never what the bytes already are
```

## Description    {#description}
Needs `use Osysharp.Storage;`. It reads only its arguments, so it answers the same for the same bytes and name. It
runs on the server, where an upload's bytes are.

How it decides, in order:

| the bytes | answer | the name |
|---|---|---|
| a known signature — PNG, JPEG, GIF, WebP, AVIF, HEIC, PDF, MP4, WebM, Ogg, MP3, FLAC, WAV | that type (`image/png`, `application/pdf`, …) | ignored: a PNG named `site.css` is `image/png` |
| a ZIP container | `application/zip` | a ZIP-family name gives its kind: `.docx`, `.xlsx`, `.pptx`, `.odt`, `.ods`, `.odp`, `.epub` |
| text that begins with `<` | `text/html`, `image/svg+xml` or `application/xml` | ignored: markup never passes as text, a stylesheet or a script |
| other text | `text/plain` | `.css` → `text/css`, `.js`/`.mjs` → `text/javascript`, `.json`/`.map` → `application/json`, `.md` → `text/markdown`, `.csv`, `.tsv`, `.yaml`/`.yml`, `.html`, `.svg`, `.xml` |
| binary with no known signature | `application/octet-stream` | `.woff`, `.woff2`, `.ttf`, `.otf`, `.ico` give their type; a name claiming a kind that HAS a signature (`.png`) does not |

So a caller cannot make a web page pass as a stylesheet or a picture by naming it so, and a picture sent as
`application/octet-stream` is still `image/png`. Compare it with the type the caller claimed when the two must agree:

```osy title="type each upload by its bytes, and refuse one whose claim is wrong" test app=storage-content-type-of
app Boards { use Osysharp.Storage; }

using Osysharp.Storage;

/// The type an upload really has; refused when the caller claimed a different one.
[AllowAnonymous]
public string TypeOf(UploadedFile upload) {
  if (!File.IsOwnUpload(upload.Path)) { throw new ValidationException("That file was uploaded somewhere else."); }
  var type = File.ContentTypeOf(File.ReadAllBytes(upload.Path), upload.FileName);
  if (upload.ContentType != "" && upload.ContentType != "application/octet-stream" && upload.ContentType != type) {
    throw new ValidationException($"{upload.FileName} is {type}, not {upload.ContentType}.");
  }
  return type;
}
```

```osy title="a PNG is a PNG whatever it is called; a script is typed by its name" run app=storage-content-type-of
[Test]
void A_file_is_what_its_bytes_are() {
  byte[] png = [137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
  Assert.Equal("image/png", File.ContentTypeOf(png, "site.css"));
  Assert.Equal("text/javascript", File.ContentTypeOf(Encoding.UTF8.GetBytes("console.log(1);"), "app.js"));
  Assert.Equal("text/html", File.ContentTypeOf(Encoding.UTF8.GetBytes("<!doctype html><p>hi</p>"), "notes.txt"));
  Assert.Equal("text/plain", File.ContentTypeOf(Encoding.UTF8.GetBytes("just words")));
}
```

## See also       {#see-also}
- [File.IsOwnUpload and File.SaveUpload](https://osysharp.com/reference/storage/own-upload/) — uploads, one or several, and whose they are
- [A file's history — supersede, and what each version keeps](https://osysharp.com/reference/storage/file-versions/) — keeping a file, and reading each version
- [Files (addressing something the app stores)](https://osysharp.com/reference/storage/index/) — the two ways the platform stores a file
