Fields

File

Any file from the media library, like a PDF.

A file field holds a file from the media library, like a PDF, a ZIP or a video. For images with responsive sizes, use an image field.

The file field in the admin

Example

cms/collections/events.ts
export const events = defineCollection({
  fields: {
    title: { type: "text", required: true },
    programme: { type: "file", accept: ["application/pdf"], folder: "programmes" },
    downloads: { type: "file", many: true, maxSize: 50 * 1024 ** 2 },
  },
});

A read loads the file, like a relation:

{
  id: "m7",
  url: "/_tenon/media/files/programmes/programme.pdf",
  alt: null,
  mimeType: "application/pdf",
  size: 182044,
  folder: "programmes",
  filename: "programme.pdf",
  key: "programmes/programme.pdf",
}
  • url is the file itself. Link to it, or pass it to a <video> or <iframe>.
  • With populate: false, the field holds the id.

Render it

{event.programme && (
  <a href={event.programme.url} download>
    Programme ({Math.round(event.programme.size / 1024)} KB)
  </a>
)}

<ul>
  {event.downloads?.map((file) => (
    <li><a href={file.url} download>{file.filename}</a></li>
  ))}
</ul>

Deleting a file

A file that a field uses cannot be deleted. The delete fails with a 409 and lists the documents that use it. Set onDelete: "set-null" to clear the field instead.

Options

Store a list of files instead of one. With required, at least one. Default false.The allowed MIME types, like ["application/pdf"]. image/* works. Default: all types that media.accept allows.The largest file in bytes. It narrows media.maxSize.The folder that Upload new in the field puts the file in, like "downloads". Default: the root.What happens when the file is deleted. Default "restrict". A required field cannot use "set-null".