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:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Blank File Uploader</title>
</head>
<body>
<div class="uploader">
<h1>File Upload</h1>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="files" webkitdirectory mozdirectory multiple>
<button>Submit</button>
</form>
</div>
</body>
</html>
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!