4

I've looked at a lot of answer for this same question, but I haven't found a working solution yet. I am trying to make a web app that you can upload files to using express and multer, and I am having a problem that no files are being uploaded and req.file is always undefined.

Express and multer version as: "express": "^4.15.4", "multer": "^1.3.0"

My configure.js looks this:

multer = require('multer');

module.exports = function(app) {
app.use(morgan('dev'));
app.use(multer({
    dest: path.join(__dirname, 'public/upload/temp')}).single('file'));
routes(app);
app.use('/public/', express.static(path.join(__dirname, '../public')));

Consuming code looks like this:

    var tempPath = req.file.path,
        ext = path.extname(req.file.name).toLowerCase(),
        targetPath = path.resolve('./public/upload/' + imgUrl + ext);

        if (ext === '.png' || ext === '.jpg' || ext === '.jpeg' || ext === '.gif') {
            fs.rename(tempPath, targetPath, function(err) {
                if (err) throw err;

                res.redirect('/images/' + imgUrl);
            });
        } else {
            fs.unlink(tempPath, function() {
                if (err) throw err;

                res.json(500, {error: 'Only image files are allowed.'});
            });
        }

The form looks like this:

    <form method="post" action="/images" enctype="multipart/form-
data">
<div class="panel-body form-horizontal">
    <div class="form-group col-md-12">
        <label class="col-sm-2 control-label"
        for="file">Browse:</label>
        <div class="col-md-10">
            <input class="form-control" type="file"
            name="file" id="file">
        </div>
    </div>

4 Answers 4

4

I have encountered the same problem. But then I change the multer version back to 0.1.8. Everything works fine. You may need to alter the package.json file: "multer": "^0.1.8"

Sign up to request clarification or add additional context in comments.

Comments

0

I bet your error message is on this line

    var tempPath = req.file.path,

because it's the only time you ask for "path" as a property of an object. Your problem seems pretty clear, you have no file in your request (AKA the object called "req"). You should log or inspect the object called req when in your consuming code to make sure you have a file within the request.

3 Comments

Yes, you are right the file is undefined. Can you suggest what I am missing here ?
Try to do the following: console.log(JSON.stringify(req)) Then look at the object, it may help you. I have not enough things to tell you what's wrong, but if I were I would check the names of the attributes (maybe you did a typo or the attribute you seek is not called that way), I would set several console.log to ensure that what is collected/sent/received is really what you think it is. If you still can't find out, upload a working (I mean a runnable ;) ) example so that we can debug the code.
Here is the code, github.com/hemanik/imgPloadr. It's an example from this book
0

For ReferenceError: path is not defined

add this line

const path = require('node:path');

Hope this will work fine.

Comments

-1

I found that the error "Cannot read property path of undefined" when using multer in find upload was due to the fileFilter option passed in, I was using File I had allowed file of type .png and .jpg only and I kept sending to FormData() file of type jpeg.

Of course this might not be the problem in your case, but worth looking into if you're facing a similar challenge.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.