Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Can't set fileSize limit to Upload collection

default discord avatar
gonzamoiguer3 years ago
4 1

I'm a bit confused about the docs. I understand how to set a global limit but I want different limits in different upload collections: ie I have an images collection and a videos collection. In my videos collection I tried adding both filesize and fileSize (different case in the docs) in upload but it throws errors. Then there is an example in the global config in which it's inside a limit object, it also throws errors.

Videos.js

...
access: {},
upload: {
  mimeTypes: ['video/mp4'],
  fileSize: 5000000, // 5MB, written in bytes
}

Throws Collection "videos" > "upload.fileSize" is not allowed

Videos.js

...
access: {},
upload: {
  mimeTypes: ['video/mp4'],
  limits: {
    fileSize: 5000000, // 5MB, written in bytes
  }
}

Also throws Collection "videos" > "upload.limits" is not allowed

What would the correct way to declare this be? Thanks in advance, 💖 payload

  • Selected Answer
    discord user avatar
    jmikrut
    3 years ago

    Hey @gonzam88 — I see what you're looking to do. And I think this is a totally reasonable request.

    As you've noticed, you can only specify global upload filesize limitations, but you can extend the built-in filesize field with a filesize field of your own, complete with a custom validation function that will enforce max upload sizes for you.

    Here's the built-in filesize field:

    const filesize = {
        name: 'filesize',
        label: 'File Size',
        type: 'number',
        admin: {
          readOnly: true,
          disabled: true,
        },
      };

    Just add that to your collection's base level with a custom validation function and then you're off to the races!

    4 replies
  • default discord avatar
    alexisbrouillette3 years ago

    I don't understand how to implement this. Can you give a bit more informations on where to add the filesize const in the collection and where to use it?

  • default discord avatar
    NamaanDev8 months ago

    you can add this thing into payload config ts file

    upload: {
    limits: {
    fileSize: Number(process.env.MAX_FILE_SIZE) || 10 * 1024 * 1024,
    },
    },

  • default discord avatar
    arfianadam7 months ago

    you can add this thing into payload config ts file

    This only apply the limit globally. I think what @alexisbrouillette was asking was a way to set it on collection level.

    @jmikrut if this is something that is great to have, I can prepare a PR. Basically what I have in mind:

    • new option for the upload field
    • set the logic in packages/ui/src/elements/Upload/index.tsx#Upload_v4#handleFileChange and do toast.error when selected file is bigger than the size limit

    What do you think?

  • default discord avatar
    mckenzie-mrilast week

    Any updates on this? Running into an issue where if a file is upload that is too big/very big- it just throws a timeout error.

    I see the upload: { limits: { ... } } in the docs. Is that not available on the collection level? I saw the limits and thought, perfect- I'll apply this to each collection where we're doing this (each collection has a different upload size limit). However, I get the following error:

    Object literal may only specify known properties, and 'limits' does not exist in type 'UploadConfig'.ts(2353) types.d.ts(558, 5): The expected type comes from property 'upload' which is declared here on type 'CollectionConfig'

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.