node.js file upload typeError Can not read property 'image'

machu pichu

I am using this program example to upload a image into uploads/fullsize directory. The application is running but if I try to upload a image I am getting the following failure:

TypeError: Cannot read property 'image' of undefined at Object.handle (/Users/machupicchu/Desktop/test/app.js:46:23) at next_layer (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/route.js:103:13) at Route.dispatch (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/route.js:107:5) at c (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:195:24) at Function.proto.process_params (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:251:12) at next (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:189:19) at next (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:166:38) at next (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:166:38) at Layer.staticMiddleware [as handle] (/Users/machupicchu/Desktop/test/node_modules/express/node_modules/serve-static/index.js:55:61) at trim_prefix (/Users/machupicchu/Desktop/test/node_modules/express/lib/router/index.js:226:17)

The application looks like this:

var express = require('express');
var bodyParser = require('body-parser');
var path = require ('path');
var port = 3000;
var fs = require('fs');


//var collection;
//dataExt = require('./routes/serverExtend');
// setup middleware
var app = express();
app.use(bodyParser());
app.use(express.static(__dirname + '/public')); //setup static public directory

app.set('views', __dirname + '/views'); //optional since express defaults to CWD/views
app.set('view engine', 'ejs');

// Start server
app.listen(port);
console.log('App started on port ' + port);


var form = "<!DOCTYPE HTML><html><body>" +
"<form method='post' action='/upload' enctype='multipart/form-data'>" +
"<input type='file' name='image'/>" +
"<input type='submit' /></form>" +
"</body></html>";

app.get('/', function (req, res){
    res.writeHead(200, {'Content-Type': 'text/html' });
    res.end(form);  
});

app.get('/uploads/fullsize/:file', function (req, res){
    file = req.params.file;
    var img = fs.readFileSync(__dirname + "/uploads/fullsize/" + file);
    res.writeHead(200, {'Content-Type': 'image/jpg' });
    res.end(img, 'binary');

});

/// Post files
app.post('/upload', function(req, res) {

    fs.readFile(req.files.image.path, function (err, data) {

        var imageName = req.files.image.name

        /// If there's an error
        if(!imageName){

            console.log("There was an error")
            res.redirect("/");
            res.end();

        } else {

          var newPath = __dirname + "/uploads/fullsize/" + imageName;

          /// write file to uploads/fullsize folder
          fs.writeFile(newPath, data, function (err) {

            /// let's see it
            res.redirect("/uploads/fullsize/" + imageName);

          });
        }
    });
});

Can someone help me why I am getting the TypeError?? Also created my directory where the files should be save. app.js uploads -> fullsize -> images.jpg

SamT

You need multipart middleware for your file upload to work as body-parser does not provide it.

First, add this to the top of your app:

var multipart = require('connect-multiparty');

Next, edit your post route slightly:

app.post('/upload', multipart(), function(req, res) {
   // ... 
}

Now req.files should now be avaiable to you in your POST /upload route.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Node.js TypeError: Cannot read property 'path' of undefined

分類Dev

TypeError: Cannot read property 'image' of undefined

分類Dev

Best implementation of: upload image file into database and read image from database

分類Dev

Vue.js - How can I solve [Vue warn]: Error in render: "TypeError: Cannot read property 'port' of undefined"?

分類Dev

What does the "Uncaught TypeError: Cannot read property" JS error mean?

分類Dev

Node.js upload file into Buffer instead of /tmp

分類Dev

Kendo UI Angular Upload File to Backend as Node.JS

分類Dev

Kendo UI Angular Upload File to Backend as Node.JS

分類Dev

Cannot read property 'join' of undefined in Javascript file Electron JS

分類Dev

ERROR TypeError: Cannot read property 'property' of undefined

分類Dev

sitejavascript.js:1 Uncaught TypeError: Cannot read property 'addEventListener' of null at sitejavascript.js:1

分類Dev

Proper Way to Read a Data File in Javascript/Node.js?

分類Dev

React/Redux Tools.js:9 Uncaught TypeError: Cannot read property 'map' of null

分類Dev

topojson/d3.js: Uncaught (in promise) TypeError: Cannot read property 'type' of undefined

分類Dev

joint.shapes.uml.js: Uncaught TypeError: Cannot read property 'Generic' of undefined

分類Dev

Three.js + OrbitControls - Uncaught TypeError: Cannot read property 'addEventListener' of undefined

分類Dev

Node JS/Gzip: Image file download ends prematurely with no error

分類Dev

Can not read image in opencv

分類Dev

TypeError: Cannot read property 'openDatabase' of undefined

分類Dev

TypeError: Cannot read property 'get' of undefined Angularjs

分類Dev

TypeError: Cannot read property '$auth' of undefined Vuejs

分類Dev

TypeError: cannot read property of undefined (ExpressJS/POST)

分類Dev

TypeError: Cannot read property 'controls' of undefined

分類Dev

TypeError: Cannot read property 'data' of undefined - but it is defined

分類Dev

ERROR TypeError: Cannot read property 'category' of undefined

分類Dev

React TypeError: Cannot read property 'replace' of undefined

分類Dev

TypeError: Cannot read property 'classList' of null

分類Dev

REACT TypeError: Cannot read property 'value' of undefined

分類Dev

ERROR TypeError: Cannot read property 'grower' of undefined

Related 関連記事

  1. 1

    Node.js TypeError: Cannot read property 'path' of undefined

  2. 2

    TypeError: Cannot read property 'image' of undefined

  3. 3

    Best implementation of: upload image file into database and read image from database

  4. 4

    Vue.js - How can I solve [Vue warn]: Error in render: "TypeError: Cannot read property 'port' of undefined"?

  5. 5

    What does the "Uncaught TypeError: Cannot read property" JS error mean?

  6. 6

    Node.js upload file into Buffer instead of /tmp

  7. 7

    Kendo UI Angular Upload File to Backend as Node.JS

  8. 8

    Kendo UI Angular Upload File to Backend as Node.JS

  9. 9

    Cannot read property 'join' of undefined in Javascript file Electron JS

  10. 10

    ERROR TypeError: Cannot read property 'property' of undefined

  11. 11

    sitejavascript.js:1 Uncaught TypeError: Cannot read property 'addEventListener' of null at sitejavascript.js:1

  12. 12

    Proper Way to Read a Data File in Javascript/Node.js?

  13. 13

    React/Redux Tools.js:9 Uncaught TypeError: Cannot read property 'map' of null

  14. 14

    topojson/d3.js: Uncaught (in promise) TypeError: Cannot read property 'type' of undefined

  15. 15

    joint.shapes.uml.js: Uncaught TypeError: Cannot read property 'Generic' of undefined

  16. 16

    Three.js + OrbitControls - Uncaught TypeError: Cannot read property 'addEventListener' of undefined

  17. 17

    Node JS/Gzip: Image file download ends prematurely with no error

  18. 18

    Can not read image in opencv

  19. 19

    TypeError: Cannot read property 'openDatabase' of undefined

  20. 20

    TypeError: Cannot read property 'get' of undefined Angularjs

  21. 21

    TypeError: Cannot read property '$auth' of undefined Vuejs

  22. 22

    TypeError: cannot read property of undefined (ExpressJS/POST)

  23. 23

    TypeError: Cannot read property 'controls' of undefined

  24. 24

    TypeError: Cannot read property 'data' of undefined - but it is defined

  25. 25

    ERROR TypeError: Cannot read property 'category' of undefined

  26. 26

    React TypeError: Cannot read property 'replace' of undefined

  27. 27

    TypeError: Cannot read property 'classList' of null

  28. 28

    REACT TypeError: Cannot read property 'value' of undefined

  29. 29

    ERROR TypeError: Cannot read property 'grower' of undefined

ホットタグ

アーカイブ