简体   繁体   中英

using express I am trying to upload file but I am getting error like this: TypeError: Cannot read property 'file' of undefined

Actually, I am trying to upload a file of any type. But I am getting error. THe HTML form part code:

<form
      action="http://localhost:3000/file_upload"
      method="POST"
      enctype="multipart/form-data">
      <input type="file" name="file" size="50" />
      <br />
      <input type="submit" value="Upload File" />
</form>

The server.js code (main entering point into the app):

var express = require("express");
var app = express();
var fs = require("fs");

var bodyParser = require("body-parser");
var multer = require("multer");

app.use(express.static("public"));
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var upload = multer({ dest: "/tmp/" });

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

app.post("/file_upload", function (req, res) {
  console.log(req.files.file.name);
  console.log(req.files.file.path);
  console.log(req.files.file.type);
  var file = __dirname + "/" + req.files.file.name;

  fs.readFile(req.files.file.path, function (err, data) {
    fs.writeFile(file, data, function (err) {
      if (err) {
        console.log(err);
      } else {
        response = {
          message: "File uploaded successfully",
          filename: req.files.file.name,
        };
      }

      console.log(response);
      res.end(JSON.stringify(response));
    });
  });
});

var server = app.listen(3000, () => {
  console.log(`Server is listening at 3000`);
});

The error I am getting is:

TypeError: Cannot read property 'file' of undefined at C:\Users\ravis\Desktop\tutorial\express\server.js:87:25 at Layer.handle [as handle_request] (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\layer.js:95:5) at C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\index.js:275:10) at serveStatic (C:\Users\ravis\Desktop \tutorial\express\node_modules\serve-static\index.js:75:16) at Layer.handle [as handle_request] (C:\Users\ravis\Desktop\tutorial\express\node_modules\express\lib\router\layer.js:9

Directory screenshot: Dirctory structure

In fs.writeFile, file isn't declared. Try defining file after the imports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM