Creating a custom /upload endpoint in my controller

System Information
  • Strapi Version: 4.10.5
  • Operating System: Windows Server 2019
  • Database: MySQL
  • Node Version: v18.13.0
  • NPM Version: 8.19.3
  • Yarn Version: -

Hi,

How can i create custom upload endpoint? I am looking for the strapi upload service where I can POST to from my React Application with axios like:

const uploadSignedPdf = async (offerte, blob, filename) => {
      var formData = new FormData();
      formData.append('files', blob, filename);
      try {
          const response = await api.post('/offertes/upload', formData, {
              headers: { 
                  "Content-Type": "multipart/form-data"
              }
          });
          if (response.data) {
              console.log(response.data);
          }
      } catch (error) {
          console.log(error)
      }
}

So it is for my offerte model. This code works fine when using the default strapi /upload endpoint but how can I create it custom for me because I want to do some extra things in it.

I already added the custom route:

{
    method: 'POST',
    path: '/offertes/upload',
    handler: 'offerte.uploadSignedOfferte',
    config: {
        controllers: [
            "api::offerte.offerte"
        ]
    }
},

I have managed to fix it. Here is what I have done:

async uploadSignedOfferte(ctx) {
	  try {
		const {
		  request: { body, files: { files } = {} },
		} = ctx;
		
			const data = {
			  fileInfo: { caption: undefined, alternativeText: undefined, name: undefined }
			};
			if (Array.isArray(files)) {
			  data.fileInfo = data.fileInfo || [];
			  data.fileInfo = files.map((_f, i) => ({ ...data.fileInfo[i], folder: 1 }));
			} else {
			  data.fileInfo = { ...data.fileInfo, folder: 1 };
			}
			if (files) {
			  const uploadedFile = await strapi.service("plugin::upload.upload").upload({
				  data,
				  files
			  });
			  return uploadedFile;
			} else {
				return ctx.send({ error: 'There is no file uploaded' }, 400);
			}
	  } catch (error) {
		console.error(error);
		return ctx.send({ error: 'Error uploading files' }, 500);
	  }
	}

Number 1 is my API upload folder ID. Hope someone find this helpful because Strapi documentation doesn’t mention anything about it.

where I can write this code? also, can you share the full code

Hi jees47,

This code is being placed in my /src/api/offerte/01-custom-offerte.js:

module.exports = {
    routes: [
		{
            method: 'POST',
            path: '/offertes/upload/:uuid',
            handler: 'offerte.uploadSignedOfferte',
            config: {
                controllers: [
                    "api::offerte.offerte"
                ]
            }
        }
    ]
}

The uploadSignedOfferte function is placed in my /src/api/offerte/controller.js

can this solve this https://forum.strapi.io/t/adding-existing-s3-bucket-images-to-strapi-project/33844

i don’t know if this code will standard upload the images to your s3 bucket. I think you have to check it. I would say if you have configured that as your uploads folders it should work but you have to test it.