0

I am trying to upload a form with JQuery Form plugin to a Node.Js + ExpressJS server ( http://malsup.com/jquery/form/#download ) ,but I'm getting this error:

Parsing file
Parsing done
Saving file
{ domain: null,
  _events: {},
  _maxListeners: 10,
  size: 861,
  path: 'C:\\Users\\Me\\AppData\\Local\\Temp\\d85fe6075dabfc8a69e978b40eed14c6.html',
  name: 'feed.html',
  type: 'text/html',
  hash: null,
  lastModifiedDate: Fri Dec 06 2013 19:43:51 GMT+0530 (India Standard Time),
  _writeStream: 
   { _writableState: 
      { highWaterMark: 16384,
        objectMode: false,
        needDrain: false,
        ending: true,
        ended: true,
        finished: true,
        decodeStrings: true,
        defaultEncoding: 'utf8',
        length: 0,
        writing: false,
        sync: false,
        bufferProcessing: false,
        onwrite: [Function],
        writecb: null,
        writelen: 0,
        buffer: [] },
     writable: true,
     domain: null,
     _events: {},
     _maxListeners: 10,
     path: 'C:\\Users\\Me\\AppData\\Local\\Temp\\d85fe6075dabfc8a69e978b40eed14c6.html',
     fd: null,
     flags: 'w',
     mode: 438,
     start: undefined,
     pos: undefined,
     bytesWritten: 861,
     closed: true } }
Saving done
Showing Errors
{ [Error: ENOENT, rename 'C:\Users\Me\AppData\Local\Temp\d85fe6075dabfc8a69e978b40eed14c6.html']
  errno: 34,
  code: 'ENOENT',
  path: 'C:\\Users\\Me\\AppData\\Local\\Temp\\d85fe6075dabfc8a69e978b40eed14c6.html' }
127.0.0.1 - - [Fri, 06 Dec 2013 14:13:51 GMT] "POST /upload HTTP/1.1" 400 61 "http://localhost:8080/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"
Showing Errors complete

The client side is working fine, but the server side seems to say that the file needs to be renamed. Although I looked through the directory and the file did not exist there at all after I tried uploading. Having a tough time decoding this error: Here's the server side code:

var fs = require('fs');
var util = require('util');
var path = require('path');
var formidable = require('formidable');

var upload = {

    parse: function parse(req, res, next) {
        console.log("Parsing file");
        var form = new formidable.IncomingForm();
        form.encoding = 'utf-8';
        form.uploadDir = process.env.TMP || process.env.TMPDIR || process.env.TEMP || '/tmp' || process.cwd();
        form.keepExtensions = true;
        form.type = 'multipart';
        form.parse(req, function (err, fields, files) {
            req.files = files;
            next(err);
        });
        console.log("Parsing done");
    },

    save: function save(req, res, next) {

        console.log("Saving file");
        // validate if upload was successful
        if (!req.files || !req.files.userfile) return next(new Error('Upload data not received, can\'t proceed.'));

        var userfile = req.files.userfile;
        // examine this object for available attributes
        console.log(userfile);

        // ensure public/data dir exists
        var dataDir = 'public/data';
        var target = path.join(dataDir, userfile.name);

        fs.rename(userfile.path, target, function (err) {
            req.uploadLink = target.replace(/public/gi, '');
            next(err);

            // cleanup
            fs.unlink(userfile.path, function () {});
        });
        console.log("Saving done");
    },

    respond: function respond(req, res, next) {
        console.log("Responding");
        var response = {
            result: 'success',
            upload: req.uploadLink,
            message: 'File uploaded!'
        };
        res.status(200).json(response);
        console.log("Response complete");
    },

    errors: function errors(err, req, res, next) {
        console.log("Showing Errors");
        console.log(err);
        var response = {
            status: 'failure',
            message: 'File upload failed!'
        };
        res.status(400).json(response);
        console.log("Showing Errors complete");
    }
};

module.exports = upload;
4
  • Can you manually create data dir under public folder, and try? Commented Dec 6, 2013 at 15:19
  • You are removing the file post upload in save(), fs.unlink(userfile.path, function () {}); hence you're unable to find the file in the file system. Comment this line out and try. Commented Dec 6, 2013 at 15:22
  • Not sure what the purpose of this line is. The code works equally well, with or without this line. What exactly does it try to "cleanup"? Commented Dec 6, 2013 at 15:50
  • Ok, this line might seem redundant, but in case if fs.rename() failed, the unlink() call will remove the temporary file created during file upload. You can keep it or remove it, your call. Commented Dec 6, 2013 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.