How to upload folders and keeping the folder structure with Node.js?
07:41 24 Apr 2021

I'm currently trying to build a File Uploader with Node.js and it works fine for single files and multiple files, I need to upload folders tho. The folder structure within the uploaded folder should stay intact.

Basically what I want is an uploader for players to upload their folders so that I can use them in the backend.

I followed this tutorial to get it to work with single and multiple files: https://www.youtube.com/watch?v=ysS4sL6lLDU

I don't understand how I can upload folders, I can "webkitdirectory mozdirectory" on my input but then it just uploads the folder content and removes the folder structure.

Here is my code:




    
    
    
    
    Blank File Uploader


    

File Upload

const express = require('express');
const multer = require('multer');
const uuid = require('uuid').v4;

const storage = multer.diskStorage({ 
    destination: (req, file, cb) => {
        cb(null, 'uploads');
    },
    filename: (req, file, cb) => {
        const { originalname } = file;
        cb(null, originalname);

        
    }
})
const upload = multer({ storage });

const app = express();
app.use(express.static('public'));

app.post('/upload', upload.array('files'), (req, res) => {
    return res.json({
        status : 'OK',
        uploaded: req.files.length,
        fileName: req.files
    })
    console.log('Start decompressing!');
    decompress(upload.storage, 'dist').then(files => {
        console.log('done!');
    });
});

app.listen(3001, () => console.log('App is listening...'));

How are other companies doing this?

I tried to upload zip files and unzip it with node.js but couldn't get it to work. Is there any way to upload folders as a whole or do I have to zip it before uploading maybe?

Please help me out.

Thank you very much!

javascript html node.js file-upload multer