Lädt...

🔧 Upload File in NextJS without any Library 🌟


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

🌟 Upload File in NextJS without any Library 🌟

So you're working on a Next.js project and need to implement a file upload feature without using any external libraries? 📁 No problem! Next.js provides a straightforward way to achieve this. Let's dive into the code!

Create the Frontend Part

"use client"
import { useState } from "react"

export default function Home() {
  const [file, setFile] = useState()

  const onSubmit = async (e) => { 
    e.preventDefault();
    console.log(file);

    let data = new FormData();
    data.set("file", file)

    let res = await fetch("/api/upload/route", {
      method: "POST",
      body: data,
    })

    let response = await res.json()
    console.log(response);
  }

  return (
    <>
      <form onSubmit={onSubmit}>
        <input type="file" onChange={(e) => setFile(e.target.files[0])} />
        <button type="submit">Upload</button>
      </form>
    </>
  )
}

Create API file api/upload/route.js

import { writeFile } from 'fs/promises'

async function handler(req, res, next) { 
  let data = await req.formData()
  console.log(data);

  data = data.get("file");
  let bytedata = await data.arrayBuffer()
  let buffer = Buffer.from(bytedata)
  let path = "./public/uploads/" + data.name;

  await writeFile(path, buffer);
  return Response.json("hi:hi")
}

export { handler as POST }

That's it! 🎉 Now you have a basic file upload setup in your Next.js app. When a user selects a file and hits the "Upload" button, the file is sent to the /api/upload/route endpoint, where it's processed and saved to the public/uploads directory.

If you need more guidance, you can check out this video tutorial for a visual walkthrough. Happy coding! 🚀

...

🔧 Upload File in NextJS without any Library 🌟


📈 45.66 Punkte
🔧 Programmierung

🔧 3 Exciting Improvements Between NextJS 14 And NextJS 13


📈 26.23 Punkte
🔧 Programmierung

🔧 NextJS examples: 60 popular websites built with NextJS and headless CMS


📈 26.23 Punkte
🔧 Programmierung

🔧 What is NextJs?  Why Should You Use it in 2023?  NextJs Pros and Cons Guide


📈 26.23 Punkte
🔧 Programmierung

🐧 Super UEFIinSecureBoot Disk. Boot any OS or launch any efi file without disabling UEFI Secure Boot.


📈 26.2 Punkte
🐧 Linux Tipps

🐧 Super UEFIinSecureBoot Disk. Boot any OS or launch any efi file without disabling UEFI Secure Boot.


📈 26.2 Punkte
🐧 Linux Tipps

🕵️ CVE-2024-6083 | PHPVibe 11.0.46 Media Upload Page upload-mp3.php file unrestricted upload


📈 23.57 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2023-1970 | yuan1994 tpAdmin 1.3.12 Upload.php Upload file unrestricted upload


📈 23.57 Punkte
🕵️ Sicherheitslücken

🔧 How to Increase File Upload Size in Web Applications (Nextjs, Express.js, Nginx, Apache)


📈 23.55 Punkte
🔧 Programmierung

🕵️ Low CVE-2019-13147: Audio file library project Audio file library


📈 22.47 Punkte
🕵️ Sicherheitslücken

🎥 Get Any Wi-Fi Password Without Any Cracking Using Wifiphisher's Social Engineering Attack [Tutorial]


📈 22.34 Punkte
🎥 IT Security Video

🔧 JWT Token authorisation in spring boot 3 without any external library


📈 22.12 Punkte
🔧 Programmierung

🔧 Estimate the read time of an article without any library in JavaScript.


📈 22.12 Punkte
🔧 Programmierung

🔧 Integrate Pop up in JavaScript without any Library


📈 22.12 Punkte
🔧 Programmierung

⚠️ jQuery-File-Upload 9.22.0 Arbitrary File Upload


📈 20.86 Punkte
⚠️ PoC

💾 jQuery-File-Upload 9.22.0 Arbitrary File Upload


📈 20.86 Punkte
💾 IT Security Tools