1

I have the file submission form on the index.html file. When the file selected, i need to say 'file uploaded', else need to display 'please upload the file'

app.js

    const express = require("express");
    const app = express();
    const http = require("http").Server(app).listen(3000);

    app.use(express.urlencoded());
    app.use(express.json());

    console.log("Server Started");

    app.get("/", function (req, res) {
    res.sendFile(__dirname + "/index.html");
    }
    )

    app.post("/", function (req, res) {
    if (req.files) {

        console.log(req.files);
        const file = req.files.filename;
        const filename = file.name;

        if (!filename) {
        res.send("Please select the file to upload");
        }
        else {
        res.send("uploaded");
        }
    }
    })

index.html

    <div>
    <h1 style="align-content: center">Upload your file here!</h1>
    </div>
    <div >

    <form label="upload" method="post" enctype="multipart/form-data" action="/">
        <label> Enter reference</label>
        <input type="text" name="test_text"></input>
        <br><br>
        <input type="file" name="filename">
        <input type="submit" value="upload">
    </form>
    </div>

Error message:

TypeError: Cannot read property 'name' of undefined
    at C:\Users\Desktop\LocalGithub\uploadFileLocal-express-fileupload\app.js:24:27
0

3 Answers 3

2

The following is quoted from express docs:

In Express 4, req.files is no longer available on the req object by default. To access uploaded files on the req.files object, use multipart-handling middleware like busboy, multer, formidable, multiparty, connect-multiparty, or pez.

As it said, I suggest you to use multer since it's more popular and easy to use

EDIT:

If you are using "express-fileupload" middleware, you have to load it via .use() method:

const express = require("express");
const app = express();
const http = require("http").Server(app).listen(3000);
const fileUpload = require('express-fileupload');
^^^^^

app.use(express.urlencoded());
app.use(express.json());

app.use(fileUpload({
^^^
    useTempFiles : true,
    tempFileDir : '/tmp/'
}));

...

check the docs for more details:

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

4 Comments

I am using express-fileupload instead of multer! How we can parse the filename?
If you see my original post, I have already included below two lines to access the response parameters. app.use(express.urlencoded()); app.use(express.json());
I have added index.html file as well. Please check!
@Nandy urlencoded and json middlewares has nothing to do with fileUpload, it's a different package that let you upload files, so you need load it separately. I added arrows ^ to my answer to indicate where to make changes in your code
0

Please try replace file.name with req.files[0].originalname.

1 Comment

Sorry, this is not helping. Any other ideas please ?
0

You did not create the object for which attribute name is defined. object: req.files.filename, attribute: req.files.filename.name should work fine. Your code should be like this:

app.post("/", function (req, res) {
    if (req.files.filename) {   //missing filename

        console.log(req.files.filename); //missing filename
        const file = req.files.filename;
        const filename = file.name;
  ...
});
    

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.