Problem while uploading file

Asked

Viewed 37 times

-2

I am performing an operation where basically the client will be sending the images you want to the server. This way the way I am putting to occur the rescue is correct, but the user will not be saving the photos only in one place. These photos have to be saved in several directories, by choosing the user. Ex: I have students and these students have a folder for each, so this folder stores several photos and information, if I go on a student’s profile and upload a photo of it, this photo has to be saved in your directory.

And with that came the problem. I’m using Multer to perform this upload and nodejs operation.

Multer:

var multer  = require('multer')
        var storage = multer.diskStorage({
            destination: function (req, file, cb) {
            let caminhoFoto =  './public/lib/face-api/labels/Karen/'
            cb(null, caminhoFoto)
            },
            filename: function (req, file, cb) {
            cb(null, file.originalname)
            }
        })
        
    var upload = multer({ storage })

    router.post('/uploadFacial', upload.single('uploaded_file'), function (req, res) {
        console.log(req.file, req.body)
        res.redirect('back')
    });

Front:

    <form action="/alunos/uploadFacial" enctype="multipart/form-data" method="post">
        <div class="form-group">
            <input type="file" class="form-control-file d-inline" name="uploaded_file">
            <input type="text" value="{{aluno._id}}" class="form-control-file d-inline" name="aluno">
            <input type="submit" value="Enviar foto" class="btn btn-default">         
        </div>
    </form>

I was wondering if in any way it is possible to change this location that is saved the upload according to the information received from the front or if there would be some other way to be doing this operation?

1 answer

0


After a research more related to this subject, I was able to verify what I have to do, even did not know that this would be possible, if someone knows why this occurs, how it occurs, so I can understand a little more related to this.

The code of the front:

<form action="/alunos/uploadFacial/{{aluno._id}}" enctype="multipart/form-data" method="post">
            <div class="form-group">
                <input type="file" class="form-control-file d-inline w-50" name="uploaded_file">
                <input type="submit" value="Enviar foto" class="btn btn-dark">         
            </div>
        </form> 

With this I am passing the url of the post the student ID;

Multer:

    var multer  = require('multer')
        var storage = multer.diskStorage({
            destination: function (req, file, cb) {
            let caminhoFoto =  './public/lib/face-api/labels/'+req.params.type
            cb(null, caminhoFoto)
            },
            filename: function (req, file, cb) {
            cb(null, file.originalname)
            }
        })
        
    var upload = multer({ storage })

    router.post('/uploadFacial/:type', upload.single('uploaded_file'), function (req, res) {
        console.log(req.file, req.body)
        res.redirect('back')
    });

After that I can be receiving the TYPE value on the wayFoto, causing it to be saved in each student’s directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.