节点js&玉文件上传错误(500)。文件日志被证明是未定义的

时间:2014-11-26 04:49:07

标签: javascript node.js express pug

我正在尝试使用nodejs,express和jade上传网页文件。

在关注问题的同时,在应用了多个表单的网页上进行文件上传。

当我上传文件时,会出现错误消息。

“无法读取属性'uploadFile'的未定义”

javascript功能

function uploadFile(req, res) {
    console.log("this is log1");
    console.log(req.files);
    console.log(req.files.uploadFile.path);
    console.log("this is log2");
    fs.readFile(req.files.uploadFile.path, function (error, data) {
        var filePath = __dirname + "\\files\\" + req.files.uploadFile.name;
        fs.writeFile(filePath, data, function (error) {
            if (error) {
                throw err;
            } else {
                res.redirect("back");
            }
        });
    });
}

客户端代码是

table(border="1")
- each stage in list.stage
  - var packFileNameArray= stage.packFileName.split("/")
  - var packVersionArray= stage.packVersion.split("/")
  - for (var i = 0; i < packFileNameArray.length; i++)
    tr
      td(width = '150')= stage.packName
      td(width = '150')= packFileNameArray[i]
      td(width = '50')= packVersionArray[i]
      td
        form(action='/upload', method='post',enctype='multipart/form-data')
          input(type='file', name='uploadFile')
          input(type='submit', name='Upload')

对于此功能,我将这些添加到app.js文件中。

var fs = require('fs'); - &gt;这也输入了函数所在的js文件。

var serverManage = require('./routes/serverManage'); - &gt;这是功能所在的js文件。

app.post('/upload', serverManage.uploadFile); - &gt;这是在按下提交按钮时触发的

我花了两天的时间来回答。但我没有找到。

请帮帮我。

谢谢。

这是我的整个app.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var fs = require('fs');
var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

var DBconnect = require('./user_modules/DBconnect');
var DBmodel = require('./user_modules/DBmodel');
var serverManage = require('./routes/serverManage');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);
app.get('/manage', serverManage.getData);
app.post('/upload', serverManage.uploadFile);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;

P.S 2.这是我的整个serverManage.js

var mongoose = require('mongoose' );             
var DBmodel = require('../user_modules/DBmodel');
var fs = require('fs');

function getData(req, res) {
    DBmodel.findOne({'key': 'server'}).exec(function (err, data) {
            //console.log(data);
            res.render("manage", { title:"serverManage SAMPLE", list: data });
        });
}

function uploadFile(req, res) {
    console.log("this is log1");
    console.log(req.files);
    console.log(req.files.uploadFile.path);
    console.log("this is log2");
    fs.readFile(req.files.uploadFile.path, function (error, data) {
        var filePath = __dirname + "\\files\\" + req.files.uploadFile.name;
        fs.writeFile(filePath, data, function (error) {
            if (error) {
                throw err;
            } else {
                res.redirect("back");
            }
        });
    });
}

module.exports.getData = getData;
module.exports.uploadFile = uploadFile;

0 个答案:

没有答案