DEV Community

Cover image for How to upload an image to strapi

How to upload an image to strapi

Bassel Kanso on July 20, 2021

Perhaps one of the hardest things to do, and that is to figure out how to upload an image to strapi, and to link it to a specific content type. On...
Collapse
 
zyqm profile image
zyq-m • Edited

Thanks Bassel it's really helpful. Just to add a little, for strapi v4 in url you must include /api endpoint, such http://localhost:1337/api/upload. Otherwise it will throw an error.

Collapse
 
leodev9911 profile image
Leonardo Fernández • Edited

Thanks a lot for this article men, this issue almost makes me crazy😅

Collapse
 
dalvesdev profile image
Diego Alves

Hi, do you believe the same process in react native will work?

Collapse
 
bassel17 profile image
Bassel Kanso

The process concerning Strapi yes, However React Native probably uses a different way to get the files from a mobile phone.

Collapse
 
ermarkar profile image
Sunil Garg

where strapi saves the photos? to the folder that can be committed to git?

Collapse
 
bassel17 profile image
Bassel Kanso

Yes Strapi does save photos by default to a folder that can be committed with git, however I recommend you use a cloud storage provider, the same method will work. You just need to setup the storage provider with Strapi

Collapse
 
amrviernes profile image
Amr Viernes • Edited

Hi Bassel after doing this giving response 400 (Bad Request) opening the network tab and i see no data sent just nulll do you know where the problem may locate

Collapse
 
mauvis profile image
Mauricio Junqueira

Hello Amr, I am with the same problem (Bad Request). But if I use just /upload, without the api, I then get an Unauthorized.

Did you solve this?

Collapse
 
amrviernes profile image
Amr Viernes

Hi Mauricio, yes i solved this problem by using axios like this

axios({
  method: 'post',
  url: '/api/upload',
  data: formData
});
Enter fullscreen mode Exit fullscreen mode

and don't forget to check upload permission in strapi users panel for public if you want to upload with authenticated users send token in the headers with the post request.

Collapse
 
bassel17 profile image
Bassel Kanso

Hello yes the new version of Strapi, the V4 version needs to be api/upload
I will try to update the post to follow the new version

Collapse
 
im_dev_pratik profile image
imdevpratik

Thanks @bassel17 ...! but i want to help in add image with caption is it possible in strapi v4 ?