强大的上传无法正常工作:' files& quot;未定义,无错误 [英] Formidable Upload Not Working: 'files" undefined, no error

查看:63
本文介绍了强大的上传无法正常工作:' files& quot;未定义,无错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 Node入门手册中的指南,尝试使用强大的功能上传文件.遵循此代码,我有一个服务器模块,该服务器模块将 request 对象传递给requestHandler模​​块.主页加载具有以下处理程序的表单:

I'm trying to upload a file using formidable, following the tutorial in the Node Beginner Book. Following this code, I have a server modules that passes the request object to a requestHandler module. The main page loads a form with the following handler:

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路径会触发以下上传处理函数:

When the form is submitted, the /upload path triggers the following upload handler function:

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();
    });

}

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

When the upload button is clicked, however, the server crashes with the following error:

/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 .所以我在这里有些困惑.想法?

So evidently the files variable is undefined. I thought there might be an error, but no the error variable is set to null. So I'm a bit stumped here. Ideas?

推荐答案

我遇到了同样的问题.我想您会在本书结尾处的Node Beginner Book上看到这些行代码.我通过删除server.js文件上的跟踪代码来修复它:

I had same problem. I think you see those line code on Node Beginner Book, at the end of the book. I fixed it by removed follow code on server.js file:

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

Just do: 

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

route(handle, pathname, res, req);

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

Sorry for my English and best wishes!

***VinRover Nguyen***

这篇关于强大的上传无法正常工作:&amp;#39; files&amp; quot;未定义,无错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆