How to upload an image from a URL in React Next.js to Strapi

This is working for me :+1:

It downloads the image from imageURL and adds it to an existing Strapi collection item (eventId)

const myImage = await fetch(imageURL);
const myBlob = await myImage.blob();

const formData = new FormData();
formData.append('files', myBlob, imageURL);
formData.append('ref', 'api::event.event');
formData.append('refId', eventId);
formData.append('field', 'image');
console.log(formData);
const imageUploaded = await fetch(`${STRAPI_URL}/api/upload`, {
  method: 'POST',
  body: formData,
});

Early days testing, but image appeared in Strapi under the correct item.

1 Like