javascript 要上载的节点js文件出错

vof42yt1  于 2023-03-16  发布在  Java
关注(0)|答案(5)|浏览(169)

What is the problem here?
I have a node js file for file to upload action and another html file
File to upload.js

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>

Exception has occurred: Error TypeError: Cannot read property 'path' of undefined at d:\CUBIC\UI\asg\1\FileUpload.js:9:39 at IncomingForm. (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:105:9) at emitNone (events.js:86:13) at IncomingForm.emit (events.js:185:7) at IncomingForm._maybeEnd (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:553:8) at Object.end (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:239:12) at IncomingMessage. (d:\CUBIC\UI\asg\1\node_modules\formidable\lib\incoming_form.js:130:30) at emitNone (events.js:86:13) at IncomingMessage.emit (events.js:185:7) at endReadableNT (_stream_readable.js:974:12)

cygmwpex

cygmwpex1#

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

hec6srdp

hec6srdp2#

根据HTML5的规范,如果您使用JavaScript操作文件上传控件的值字符串,则该控件不应显示所选文件的真实的本地路径,而是由处理文件信息的脚本返回的字符串C:\fakepath。
此要求已在Internet Explorer 8中实现-仅当包含控件的页面添加到浏览器的受信任站点集合时,才会显示文件的真实的路径。
如果你想替换假路径,就像这样
这是有道理的;基本上,浏览器正在输入蹩脚的C:\fakepath\ text。幸运的是,我所需要做的就是通过执行一个简单的字符串替换调用来修复这个问题:

// 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上传文件。希望这对您有所帮助。

xmakbtuz

xmakbtuz3#

使用files.fileupload.path对我很有效。显然,令人生畏的标签有一点改变。

gr8qqesn

gr8qqesn4#

我在这里加上这个答案,因为我面临着同样的问题。可能会帮助别人。
我用的是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);
nnt7mjpx

nnt7mjpx5#

确保输入类型的名称属性值和XXXXX(文件.XXXXX.文件路径)必须相同。如我命名为filetouload

const formidable = require('formidable');
const http = require('http')
const fs = require('fs')
http.createServer((req,res) =>{
    if (req.url == '/fileupload') {
        const form = new formidable.IncomingForm();
        form.parse(req,function(err,fields,files){
            var oldpath = files.filetoupload.filepath;
            var newpath = 'C:/User/FOLDER_NAME' + files.filetoupload.originalFilename;
            fs.rename(oldpath, newpath, function (err) {
            if (err) throw err;
                res.write('File uploaded and moved!');
            res.end();
        });
        })
    } else {
        res.writeHead(200,{'Content-Type':'text/html'})
        res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
        res.write('<input type="file" name="filetoupload"><br>');
        res.write('<input type="submit">');
        res.write('</form>');
        return res.end("Over")
    }
}).listen(8080)

相关问题