【问题标题】:NodeJs/Formidable, Uploading Images errorNodeJs/强大,上传图片错误
【发布时间】:2019-04-02 11:05:15
【问题描述】:

我正在尝试使用 NodeJs 和 Formidable 制作图像/文件上传表单。我没有使用快递。简直厉害。但无论如何,我尝试了最常见的强大脚本,但都没有奏效。我的代码与本教程中的代码基本完全相同:http://justinkjchang.wordpress.com/2013/11/08/image-uploader-using-node-js/ 周围有很多这样的教程,我在一些书籍或视频中看到了几乎完全相同的内容,它们似乎都有效,除了我的。我已经安装了强大的,我有最新版本,还有 Node.js。我也在mac上。当我尝试上传时,它会通过 form.parse 和所有内容,但是当它尝试写入文件时,它会抛出这个错误:

/Users/USER/Documents/Node/requestHandlers.js:42
    fs.rename(files.upload.path, "/tmp/test.png", function (error) {
                          ^
TypeError: Cannot read property 'path' of undefined

我是 Nodejs 的新手,所以任何形式的帮助都会很好。我也尝试过 file.write ,它仍然抛出同样的错误。任何帮助将不胜感激(:

更新代码:

var exec = require("child_process").exec;
var qs = require("querystring"),
fs = require("fs"),
formidable = require("formidable"),
url = require( "url" );

function start(response, request) {
/*
var fileName = "start.html"
var localPath = __dirname;
var mimeType = "text/html";
var name = localPath + "/" + fileName;
getFile(name, response, mimeType);
console.log("Request handler 'start' was called.");
console.log("Serving File: " + name);
*/
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>';

console.log( "Request for 'start' is called." );

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

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

var form = new formidable.IncomingForm();

form.parse(request, function(error, fields, files){
    if(error){
        console.log(error);
        console.log("Dun Goofed");
    }
    console.log("parsing done");
    fs.rename(files.upload.path, "/tmp/test.png", function (error) {
        if (error) {
            fs.unlink("/tmp/test.png");
            fs.rename(files.upload.path, "/tmp/test.png");
        }
    });
    /* fs.rename(files.upload.path, "/tmp/test.png", function(err){
        if(err){
            fs.unlink("/tmp/test.png");
            fs.rename(files.upload.path, "/tmp/test.png");
        }
    }); */
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Received image: <br/>");
    response.write("<img src='./show' />");
    response.end(); 
});
}


exports.start = start;
exports.upload = upload;
//exports.show = show;

我省略了一些部分,因为它们与文件上传无关,也请cmets原谅,只是旧代码。我也没有 /show 功能,因为我暂时删除了它。 此外,这是我在控制台中遇到的错误。错误之一是因为上传取消。在我输入我要上传的文件后,浏览器只是在那里坐了一会儿,等待服务器的请求,在控制台中,它只是在 form.parse 之前停止,然后坐在那里什么也不做大约一分钟,然后它推出了所有的错误。

Web Server started on 127.0.0.1:8888
Request for / received.
About to route a request for /
Served a request for /
Request for 'start' is called.
Request for /upload received.
Received POST data chunk
About to route a request for /upload
Served a request for /upload
Request handler 'upload' was called.
Preparing upload

成功执行到这里。 然后浏览器等待一会,然后要么弹出没有收到数据,要么网页不可用,然后控制台出现这些错误。

{}
[Error: Request aborted]
Dun Goofed
parsing done

/Users/USER/Documents/Node/requestHandlers.js:50
    fs.rename(files.upload.path, "/tmp/test.png", function (error) {
                          ^
TypeError: Cannot read property 'path' of undefined
at /Users/USER/Documents/Node/requestHandlers.js:50:31
at IncomingForm.<anonymous>    (/Users/USER/node_modules/formidable/lib/incoming_form.js:89:9)
at IncomingForm.EventEmitter.emit (events.js:95:17)
at IncomingForm._error (/Users/USER/node_modules/formidable/lib/incoming_form.js:272:8)
at IncomingMessage.<anonymous>     (/Users/USER/node_modules/formidable/lib/incoming_form.js:107:12)
at IncomingMessage.EventEmitter.emit (events.js:92:17)
at abortIncoming (http.js:1911:11)
at Socket.serverSocketCloseListener (http.js:1923:5)
at Socket.EventEmitter.emit (events.js:117:20)
at TCP.close (net.js:465:12)

【问题讨论】:

  • 是您的html file - upload 的名称属性吗? - &lt;file name="upload" ?
  • 哇,抱歉,我输入的内容与我看到的不同,我的错。是的,名称属性设置为上传
  • 嗯 - 原来如此 - &lt;form action="/upload" enctype="multipart/form-data" method="post"&gt;&lt;input type="file" name="upload" multiple="multiple"&gt;&lt;input type="submit" value="Upload file" /&gt;&lt;/form&gt; ?如果是这样,可能需要查看更广泛的代码
  • @RobSedgwick 是的,就是这样,我将代码添加到我的问题中以便您可以看到它。
  • 我将 Files.upload.path 更改为 files.files.path 因为我在某个地方看到它可能有效,但它没有。问题还是一样

标签: javascript node.js file-upload formidable


【解决方案1】:

我也有同样的问题。

删除server.js中的代码request.setEncoding("utf8");

像这样:

function start(route, handler) {
    http.createServer(function(request, response) {
        var postData = "";
        var pathname = url.parse(request.url).pathname;
        route(handler, pathname, response, request);

    }).listen(8889);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    相关资源
    最近更新 更多