我按照教程用的是formidable, 安装成功 引用文件没有问题,是最新版的。 根据教程和对比官方教程 就是设置不成功啊
function plug_file(req,res){//失败啊
if (req.url == '/plug_file' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received plug_file:\n\n');
res.end(sys.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form enctype="multipart/form-data" '+
'method="post">'+
'<input type="file" name="plug_file" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}
到底什么地方有错啊? 上传后的结果就是
received plug_file:
{ fields: {}, files: {} }
但是教程里面上传成功会返回N多东西 为神马啊啊啊啊啊
10 回复
设置的路径文件夹都创建了吧?这是我以前上传图片的服务端代码:
function upload(response, request){
logger.debug("Request handler 'upload' was called. ");
if (request.method.toLowerCase() == 'post'){
var form = new formidable.IncomingForm();
form.encoding = 'utf-8';
form.uploadDir = "./StaticResource/tmp"; //必须设置
form.keepExtensions = true;
form.parse(request, function(err, fields, files) {
if (err) {
logger.error("upload err:" + err);
response.end("{\"msg\":\"上传图片失败!\"}");
return;
}
logger.error("uploading " + fields.username);
fs.renameSync(files.upload.path, "./StaticResource/tmp/" + fields.username + ".jpg");
response.end("{\"msg\":\"上传图片成功!\"}");
});
}
}
还是不行啊 我又弄了很久了
if (req.url == '/plug_file' && req.method.toLowerCase() == 'post') {
var postData = '';
//测试是否上传数据成功
req.on('data',function(data){
postData+=data;
});
req.on('end',function(){
console.log(postData);
//终端能收到数据
res.end();
});
var form = new formidable.IncomingForm();
//用别人的模块上传
form.uploadDir='/var/www/static';
//form.keepExtensions = true;
form.encoding = 'utf-8';
form.parse(req, function(err, fields, files) {
//console.log('formidable_ing');
console.log(err);console.log(fields);console.log(files);
//终端输出上传结果
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received plug_file:\n\n');
res.end();
});
console.log('formidable_end');
}
终端结果是: [Error: MultipartParser.end(): stream ended unexpectedly: state = START_BOUNDARY] {} {}