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.

How to upload image file to media collection with Payload Handler?

default discord avatar
nafisaalam2 years ago
1

my req.files have two files , image and payment slip. the api is a post api.both are images. i have a collection "media" where i upload all the images. now if i want to upload images to my media collection from a payload handler , how should i do that?


{image:
{name: 'quiz 1.png', data: Buffer(6520), size: 6520, encoding: '7bit', tempFilePath: '', …}
paymentSlip:
{name: 'images.jpeg', data: Buffer(11274), size: 11274, encoding: '7bit', tempFilePath: '', …}}
  • default discord avatar
    paulpopus2 years ago

    from the request you can pass the files directly into the media create function



    const media = await payload.create({
          collection: 'media',
          data: {
            alt: `My alt text`, // <- extra fields
          },
          file: req.files.file,
        })


    then you can update the reference/use of it in a content type if you need to



    const updatedContent = await payload.update({
            id: id,
            collection: 'content',
            data: {
              picture: media.id,
            },
          })


    in my example it's not an array though, so you'll need to change that for yours

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.