3

我正在尝试按照Node Beginner Book中的教程使用强大的文件上传文件。在此代码之后,我有一个将request对象传递给 requestHandler 模块的服务器模块。主页使用以下处理程序加载表单:

function start(response) {
    console.log("Request handler 'start' was called.");

    var body = '<html>'+
        '<head>'+
        '<meta http-equiv="Content-Type" content="text/html"; '+
        'charset=UTF-8" />'+
        '</head>'+
        '<body>'+
        '<form action="/upload" enctype="multipart/form-data method="post">'+
        '<input type="file" name="upload" multiple="multiple"'+
        '<input type="submit" value="Upload file" />'+
        '</form>'+
        '</body>'+
        '</html>';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();

}

提交表单时,/upload 路径会触发以下上传处理函数:

function upload(response,request) {
    console.log("Request handler 'upload' was called.");

    var form = new formidable.IncomingForm();
    console.log("about to parse");
    form.parse(request, function(error, fields, files) {
        console.log("parsing done");

        console.log(util.inspect({error: error, fields: fields, files: files}));

        fs.rename(files.upload.path, "/tmp/test.png", function(error) {
            if (error) {
                console.log(error);
                fs.unlink("/tmp/test.png");
                fs.rename(files.upload.path, "/tmp/test.png");
            }
        });
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("received image:<br/>");
        response.write("<img src='/show' />");
        reponse.end();
    });

}

但是,当单击上传按钮时,服务器崩溃并出现以下错误:

/home/****/Coding/nodebeginner/requestHandlers.js:38
        fs.rename(files.upload.path, "/tmp/test.png", function(erro
                              ^
TypeError: Cannot read property 'path' of undefined
    at /home/****/Coding/nodebeginner/requestHandlers.js:38:25
    at IncomingForm.<anonymous> (/home/****/Coding/nodebeginner/node_modules/formidable/lib/incoming_form.js:104:9)
    at IncomingForm.EventEmitter.emit (events.js:92:17)
    at IncomingForm._maybeEnd (/home/****/Coding/nodebeginner/node_modules/formidable/lib/incoming_form.js:551:8)
    at Object.end (/home/****/Coding/nodebeginner/node_modules/formidable/lib/incoming_form.js:238:12)
    at IncomingMessage.<anonymous> (/home/****/Coding/nodebeginner/node_modules/formidable/lib/incoming_form.js:129:30)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

所以很明显这个files变量是未定义的。我认为可能有错误,但没有将error变量设置为null. 所以我在这里有点难过。想法?

4

5 回答 5

1

我也有同样的问题。您的表单标签缺少 enctype 属性上的右双引号 (")。

于 2014-10-29T16:03:12.907 回答
1

我有同样的问题。我想你会在本书末尾的 Node Beginner Book 上看到那些行代码。我通过删除 server.js 文件上的以下代码来修复它:

//      req.setEncoding("utf8");
//        req.addListener("data", function(postDataChunk) {
//            postData += postDataChunk;
//        });
//        req.addListener("end", function() {
//            route(handle, pathname, res, req);
//        });
Just do: 

路线(句柄,路径名,res,req);

And last, you must be careful at html form tags.

Sorry for my English and best wishes!

***VinRover Nguyen***
于 2015-11-12T13:22:01.390 回答
0

form.parse 中回调的参数名称有错误。

Node Beginner Book 将代码显示为:

form.parse(request, function(error, fields, files) {
    //code
}

回调函数中的参数不应该是复数:

form.parse(request, function(error, field, file) {
    //code
}

看看 /node_modlues/formidable/lib/incoming_form.js 中的 IncomingForm.prototype.parse。回调正在侦听单数 "field" 和 "file" 。

于 2017-02-18T22:25:46.663 回答
0

我被困在同一个问题上。听起来像上传类已被替换或拼写错误。尝试将引用从 files.upload.path 更改为 files.Upload.path 。它在这里工作。查看主分支以获取更多信息:Git

于 2016-03-05T19:32:08.440 回答
0

看起来您的“var body”中有一些错误。尝试 :

var body = '<html>' +
  '<head>' +
  '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
  '</head>' +
  '<body>' +
  '<form action="/upload" enctype="multipart/form-data" method="post">' +
  '<input type="file" name="upload" multiple="multiple">' +
  '<input type="submit" value="Upload File">' +
  '</form>' +
  '</bidy>' +
  '</html>';
于 2016-07-29T14:28:45.713 回答