NextJS with Strapi: Media not being uploaded on entry creation

Hey there
I have a collection named gig, and I’m trying to upload a few attachments and banners right after creating a Gig.
This is how my POST request looks like

const Formik = useFormik({
    initialValues: {
      title: "",
      category: "",
      subCategory: "",
      description: "",
      services: [],
      attributes: [],
      banners: null,
      attachments: null,
    },
    onSubmit: async (values) => {
      try {
        const formData = new FormData();
        let data = {};
        Object.keys(values)?.forEach((property, key) => {
          if (property != "banners" && property != "attachments") {
            if (property == "category" || property == "subCategory") {
              data[property] = { connect: [values[property]] };
            } else {
              data[property] = property.includes("Price")
                ? Number(values[property])
                : values[property];
            }
          } else {
            formData.append(`files.${property}`, values[property]);
          }
        });
        formData.append("data", JSON.stringify(data));
        const res = await fetch(
          `${API_BASE_URL}/gigs`,
          { 
            method: "POST",
            body: formData,
            headers: {
              Authorization: "Bearer " + Cookies.get("token"),
            },
          }
        );
        console.log(res);
        toast.success("Gig created successfully!");
      } catch (error) {
        toast.error("Couldn't create Gig");
      }
    },
  });

Everything seems working fine except the banners and attachments.
However I can see that my files are being sent. It’s just that they are not being saved in Strapi.

Can you guys please help me find out what am I doing wrong here?

I think you want to send uploaded files to /upload as a POST request. then once you get that back you can assign the ID you get back to the gigs you are doing.

1 Like

Yes that is also a solution but for that I will have to do so much extra work.
My current approach is documented already but don’t know why it’s not working.

https://docs.strapi.io/dev-docs/plugins/upload#upload-files-at-entry-creation

Had to do this way and everything seems fine now. Direct file upload while entry creation is somehow not working.