Logo

dev-resources.site

for different kinds of informations.

Upload File di Node JS

Published at
4/11/2024
Categories
node
webdev
file
upload
Author
juanangelaalma
Categories
4 categories in total
node
open
webdev
open
file
open
upload
open
Author
14 person written this
juanangelaalma
open
Upload File di Node JS

Apa itu Node JS?
Node JS merupakan run time environment pada javascript yang didesain untuk membuat aplikasi website menggunakan bahasa pemrograman javascript. Pada jaman dahulu javascript dikenal sebagai bahasa pemrograman yang berjalan di sisi client, namun sekarang javascript dapat berjalan di sisi server

Apa saja yang akan kita butuhkan?

  1. Node JS
  2. Package Manager (npm, yarn, etc)
  3. Text editor
  4. Formidable

Step 1
Install package formidable

npm install formidable
Enter fullscreen mode Exit fullscreen mode

Step 2
Buat form.html di dalam folder project node js. Form ini akan digunakan untuk field upload file

<form action="upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit">
</form>
Enter fullscreen mode Exit fullscreen mode

Step 3
Buat server.js yang akan menjadi server untuk upload file

const http = require('http')
const formidable = require('formidable')
const fs = require('fs')

const server = http.createServer((req, res) => {
  if(req.url == '/upload') {
    const form = new formidable.IncomingForm()
    form.parse(req, (err, fields, files) => {
      const oldPath = files.file.path
      const newPath = './files/'+files.file.name
      fs.rename(oldPath, newPath, (err) => {
        if(err) throw err
        res.write('File uploaded')
        res.end()
      })
    })
  } else {
    fs.readFile('./form.html', 'utf8', (err, data) => {
      if(err) throw err
      res.writeHead(200, {'Content-Type': 'text/html'})
      res.write(data)
      res.end()
    })
  }
})

server.listen(8080, () => {
  console.log('server running at 8080')
})
Enter fullscreen mode Exit fullscreen mode

yang terakhir jangan lupa untuk membuat folder file yang digunakan untuk menyimpan file yang telah diupload

upload Article's
30 articles in total
Favicon
UploadThing: A Modern File Upload Solution for Next.js Applications
Favicon
NestJS - Armazenamento nas nuvens
Favicon
NestJS - criar um endpoint para upload de diversos arquivos
Favicon
NestJS - Validando o envio de arquivos
Favicon
NestJS - Armazenamento local de upload
Favicon
NestJS - criar endpoint para upload de 1 arquivo
Favicon
Next.js: Upload de imagem para a Cloudflare R2 Utilizando Presigned URL
Favicon
A file uploader built with shadcn/ui and react-dropzone
Favicon
Elevate Your GraphQL API: Mastering File Uploads with Yoga GraphQL
Favicon
Mastering File Uploads in Laravel 11: A Comprehensive Guide
Favicon
How To Upload Multiple File In Laravel 10
Favicon
Different approaches to reduce AWS S3 file upload time using AWS-SDK v3 in NodeJS.
Favicon
How would you handle image upload?
Favicon
Upload File di Node JS
Favicon
Uploading Images with Node.js and React.js
Favicon
Simplifying AWS S3 File Uploads with the Android Library
Favicon
Laravel 10 Crop Image Before Upload Cropper Js
Favicon
Enhance Job Seeker Experience: Quick Image Upload for Portals
Favicon
A file upload sample with NextJs
Favicon
Azure Storage Account: Unleash the Potential of Cloud-Based Storage
Favicon
Spheron Network: Browser Upload
Favicon
Uploading files from terminal on file hosting service, and unlimited cloud storage
Favicon
Process FormData file sent to Node server
Favicon
Express file upload using Multer
Favicon
Maximize Your Client Upload Efficiency with the Bulk Upload
Favicon
Upload images in Ckeditor 5 with Laravel
Favicon
Eteot Logo
Favicon
Install & Upload WordPress Plugin
Favicon
Laravel 9 Multiple Image Upload Example
Favicon
How To Enhance JavaScript File Upload In Your Web App

Featured ones: