DEV Community

Lê Vũ Huy
Lê Vũ Huy

Posted on

Upload image to an entry on Strapi

Strapi is great but their document is really confusing. I spent about 2 hours trying to achieve this. When you create a new entry from REST API, you may also want to upload image to that entry.

1. REST API

Try make a POST request to

https://STRAPI_URL/api/upload
Enter fullscreen mode Exit fullscreen mode

with the form-data:

ref: the table ID (ex: `api::submission.submission`)
refId: the entry ID
field: the field to upload
files: the file to upload
Enter fullscreen mode Exit fullscreen mode

This is how we do it in Postman
Postman

2. Javascript SDK

strapi-js-sdk haven't supported upload file. Luckily, we can do it with a workaround.

const formData = new FormData();
formData.append(
    "files",
    blobFile,
    "file-name.jpg"
);
formData.append("ref", "api::submission.submission");
formData.append("refId", entryId);
formData.append("field", "speakingAudio");

const response = await strapi.request("POST", "/upload", {
    headers: {
        "Content-Type": "multipart/form-data",
    },
    data: formData,
});
Enter fullscreen mode Exit fullscreen mode

If you have a question, don't hesitate to comment below.

Top comments (0)