要上载的节点js文件出错

时间:2017-09-08 01:18:30

标签: javascript node.js

这里有什么问题?

我有一个节点js文件用于文件上传操作和另一个html文件

要上传的文件

var formidable = require('formidable');

var http = require('http');

var form = new formidable.IncomingForm();

http.createServer(function(req, res){

    form.parse(req, function(err, fields, files){
        console.log(files.filetoUpload.path);
    });
}).listen(3002);

fileUpload.html

<body>
    <form action="" enctype="multipart/form-data" method="post">

        <input type="file" name="filetoUpload">
        <input type ="submit" value="Upload">
    </form>    
</body>

  

发生异常:错误TypeError:无法读取属性'path'   未定义的       在d:\ CUBIC \ UI \ asg \ 1 \ FileUpload.js:9:39       在IncomingForm。 (d:\三次\ UI \ ASG \ 1 \ node_modules \强大\ lib中\ incoming_form.js:105:9)       在emitNone(events.js:86:13)       在IncomingForm.emit(events.js:185:7)       在IncomingForm._maybeEnd(d:\ CUBIC \ UI \ asg \ 1 \ node_modules \ formidable \ lib \ incoming_form.js:553:8)       在Object.end(d:\ CUBIC \ UI \ asg \ 1 \ node_modules \ formidable \ lib \ incoming_form.js:239:12)       在IncomingMessage。 (d:\三次\ UI \ ASG \ 1 \ node_modules \强大\ lib中\ incoming_form.js:130:30)       在emitNone(events.js:86:13)       在IncomingMessage.emit(events.js:185:7)       at endReadableNT(_stream_readable.js:974:12)

4 个答案:

答案 0 :(得分:0)

它应该是files.filetoupload.path),而且您似乎错误地将其编码为files.filetoUpload.path)(U为大写)。

希望这有帮助。

答案 1 :(得分:0)

  

根据HTML5的规范,文件上传控件应该   如果你没有透露你选择的文件的真实本地路径   用JavaScript操纵它的值字符串。相反,字符串   由脚本返回,它处理文件信息   C:\ fakepath

     

此要求已在Internet Explorer 8中实现 -   仅当包含该页面的页面时,才会显示该文件的实际路径   control被添加到浏览器的可信站点集合中。

如果你想替换像这样的虚假路径

这是有道理的;本质上,浏览器正在输入蹩脚的C:\ fakepath \ text in。幸运的是,我需要做的就是通过执行简单的字符串替换调用来解决问题:

// Change the node's value by removing the fake path
inputNode.value = fileInput.value.replace("C:\\fakepath\\", "");

您需要在upload.js文件中要求路径模块。尝试在代码var path=require('path');中使用此代码,然后使用npm install path

安装路径模块

尝试使用此代码,您可以在终端中看到浏览器响应和响应。

var multiparty = require('multiparty');
var http = require('http');
var util = require('util');
var fs = require('fs');
var path = require('path');
http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {

        var key=files.upload[0];
        fs.readFileSync(key.path);
        console.log("path":key.path);
        console.log("File name":key.originalFilename);
        res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields ,files:files.upload}));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple" id="file-id"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

node app.js运行服务器并使用http://localhost:8080上传文件希望这会有所帮助。

答案 2 :(得分:0)

我在此添加此答案,因为我遇到同样的问题,如果遇到此类错误,可以帮助其他人。

我使用了files.file.path,它在Windows系统中运行良好。

var formidable = require('formidable');

var http = require('http');

var form = new formidable.IncomingForm();

http.createServer(function(req, res){

    form.parse(req, function(err, fields, files){
        console.log(files.file.path);
    });
}).listen(3002);

答案 3 :(得分:0)

使用files.fileupload.path对我有用。显然,强大的功能改变了标签。