Next.js Discord

Discord Forum

How to get image url from the formdata in server action.

Unanswered
Tan posted this in #help-forum
Open in Discord
TanOP
I am trying upload image in cloudinary via server actions.
Here is my snippet
export const uploadToCloudinary = (formData: FormData) => {
  console.log(formData.get("image"));
/* 
Getting this output:
File {
  size: 1506744,
  type: 'image/jpeg',
  name: 'nature.jpg',
  lastModified: 1695279357083
}
*/
};

Now I want to grab the url of this image. I am not sure how can I be able to do this. Any helpful guidence please.

6 Replies

@@ts-ignore you upload this to cloudinary and then they give you the url
TanOP
export const uploadToCloudinary = (formData: FormData) => {
  const image = formData.get("image");
  cloudinary.uploader
    .upload(image)
    .then((res) => console.log(res))
    .catch((err) => console.log(err));
};

Like this ?
@@ts-ignore https://tryitands.ee
TanOP
I'll try but typescript is yelling at me. saying :
   No overload matches this call.  Overload 1 of 2, '(file: string, options?: UploadApiOptions | undefined, callback?: UploadResponseCallback | undefined): Promise<UploadApiResponse>', gave the following error.    Argument of type 'FormDataEntryValue | null' is not assignable to parameter of type 'string'.      Type 'null' is not assignable to type 'string'.  Overload 2 of 2, '(file: string, callback?: UploadResponseCallback | undefined): Promise<UploadApiResponse>', gave the following error.    Argument of type 'FormDataEntryValue | null' is not assignable to parameter of type 'string'.      Type 'null' is not assignable to type 'string'. 13:13:4 typescript2769
TanOP
Yeah fetch is there but I wanted to use the prebuilt sdk. I had used it with react where I could get url by URL.createObjectUrl(image) . Here it is also asking for the same. And I couldn't figure out how to grab the url form the file. May be due to difference in client and server component