richardgirges / express-fileupload

Simple express file upload middleware that wraps around busboy
MIT License
1.52k stars 261 forks source link

Folder upload #287

Open Spiderjockey02 opened 2 years ago

Spiderjockey02 commented 2 years ago

When I upload a folder, it will only get the actual files within the folder instead of the folder aswell? Is it possible to get the folder and then inside that the files?

router file:

// upload files to user's account
router.post('/upload', ensureAuthenticated, (req, res) => {
    if (!req.files || Object.keys(req.files).length === 0) {
        return res.status(400).send('No files were uploaded.');
    }

    // Find where to place the file
    const sampleFile = req.files.sampleFile;
    console.log(sampleFile);
    const path = req.body['path'];
    const directPath = path.split('/').slice(2, path.length).join('/');
    const newPath = location + req.user._id + '/' + directPath + '/' + sampleFile.name;

    // save file
    sampleFile.mv(newPath, function(err) {
        if (err) return res.status(500).send(err);
        res.redirect('/files/' + directPath);
    });
});

webpage file (.ejs):

<form action="/files/upload" method="post" encType="multipart/form-data" ref='uploadForm' id='uploadForm' >
    <label class="dropdown-item" type="button" id="fileHover">
        Files<input type="file" hidden name="sampleFile" onChange="javascript:document.getElementById('imagefile').click();" />
    </label>
    <input type="hidden" value="<%= path%>" name="path">
    <label class="dropdown-item" type="button" id="fileHover">
         Folders<input type="file" hidden name="sampleFile" webkitdirectory mozdirectory onChange="javascript:document.getElementById('imagefile').click();" />
    </label>
    <button type="submit" style="display:none;" id="imagefile"></button>
 </form>
BluDood commented 1 year ago

+1

EDIT: Seems like providing the preservePath: true option keeps the path in the filename.