我正在尝试将带有JQuery Form插件的表单上传到Node.Js + ExpressJS服务器(http://malsup.com/jquery/form/#download),但我收到此错误:
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
客户端工作正常,但服务器端似乎说该文件需要重命名。虽然我查看了目录,但在尝试上传后,该文件根本不存在。解码此错误很难:这是服务器端代码:
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;