🍋
Menu
File Formats

MIME Sniffing

MIME Type Sniffing

A browser behavior that examines file content to determine its type when the declared MIME type is missing or incorrect.

Detalhe técnico

MIME Sniffing follows the type/subtype pattern defined in RFC 6838. The IANA maintains the official registry of types: application, audio, font, image, message, model, multipart, text, and video. Custom types use the 'vnd.' (vendor) prefix (e.g. application/vnd.ms-excel). Web servers set the Content-Type header to the MIME type; browsers use it — not the file extension — to determine how to process the response. An incorrect MIME type (e.g. text/plain for JavaScript) will cause browsers to reject the resource due to MIME type checking.

Exemplo

```javascript
// Detect MIME type from file extension
const mimeTypes = {
  'pdf': 'application/pdf',
  'png': 'image/png',
  'jpg': 'image/jpeg',
  'svg': 'image/svg+xml',
  'json': 'application/json',
  'csv': 'text/csv',
};
const ext = filename.split('.').pop().toLowerCase();
const type = mimeTypes[ext] || 'application/octet-stream';
```

Ferramentas relacionadas

Termos relacionados