name
name

Reputation: 1

Error on node js file to upload

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)

Upvotes: 0

Views: 3033

Answers (5)

Karthik 3489
Karthik 3489

Reputation: 1

Make sure name attribute value of the input type and XXXXX (files.XXXXX.filepath) must be same. like i named as filetoupload


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)

Upvotes: 0

Pamira
Pamira

Reputation: 129

I am adding this answer here as I faced the same issue. Might help someone else.

I used files.file.path and it worked perfectly in 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);

Upvotes: 0

Sampada
Sampada

Reputation: 2991

Using files.fileupload.path worked for me. Apparently formidable has changed the labeling a bit.

Upvotes: 0

Syed Ayesha Bebe
Syed Ayesha Bebe

Reputation: 1448

According to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is C:\fakepath.

This requirement is already implemented in Internet Explorer 8 - the real path to the file will be shown only if the page that contains the control is added to the trusted sites collection of the browser.

If you want to replace fake path use like this

That made sense; essentially the browser is feeding that lame C:\fakepath\ text in. Luckily all I needed to do was fix the issue by doing a simple string replace call:

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

You need to require path module in your upload.js file . Try using this in your code var path=require('path'); and install the path module using npm install path

Try this code and you can see both the browser response and in response in terminal.

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

Run the server as node app.js and upload the file using http://localhost:8080 Hope this helps.

Upvotes: 0

David R
David R

Reputation: 15639

It should be files.filetoupload.path), and it seems you've have coded this wrongly as, files.filetoUpload.path) (with U in uppercase).

Hope this helps.

Upvotes: 0

Related Questions