Next.js Discord

Discord Forum

Send files to backend without saving them?

Unanswered
European Storm-Petrel posted this in #help-forum
Open in Discord
European Storm-PetrelOP
I'm trying to send a file to backend:
"use client";

import { addPost } from "@/app/servercomponents/actions";
import { useSession } from "next-auth/react";
import { useFormState } from "react-dom";

export default function Create() {
  const [state, formAction] = useFormState(addPost, {
    success: false,
    error: "",
  });
  return (
    <div className=" w-full h-full">
      <h1>Create</h1>
      <p>Coming Soon</p>
      <form action={async (FormData: FormData) => formAction(FormData)}>
        <input type="text" name="description" placeholder="description" />
        <input type="file" name="source" placeholder="source" />
        <input type="text" name="sound" placeholder="sound" />
        {state.success ? (
          <p>Success</p>
        ) : (
          <button type="submit">Add Post</button>
        )}
      </form>
      {state.error && state.error !== "" && <p>{state.error}</p>}
    </div>
  );
}

Then save it to my google drive account :
      const auth = new google.auth.OAuth2();
      auth.setCredentials({
        access_token: process.env.GOOGLE_CLIENT_SECRET as string,
      });
      const drive = google.drive({ version: "v3", auth });
      const fileMetadata = {
        name: (formData.get("source") as File).name,
      };
      const media = {
        mimeType: (formData.get("source") as File).type,
        // TODO: Fix this
        body: createReadStream((formData.get("source") as File).name),
      };

      const response = await drive.files.create({
        requestBody: fileMetadata,
        media: media,
        fields: "id",
      });

      const fileId = response.data.id;

But it doesnt work because it sends only some values like :
File {
  size: 12345,
  type: 'image/png',
  name: 'nameoffile',
  lastModified: 12345
}

I dont wanna save the file , i just wanna send it directly to google drive api by sending the buffer.

1 Reply

European Storm-PetrelOP
TypeError: part.body.pipe is not a function


this is the error