跟着这里的教程走到最后部分:http://www.nodebeginner.org/index-zh-cn.html
附上我的上传函数代码:
function upload(response, request){
var form = new formidable.IncomingForm();
//form.uploadDir = "tmp";
form.parse(request, function(error, fields, files) {
//var filename = path.basename(files.upload.path);
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-Type": "text/html"});
console.log(files.upload.path);
response.write("<img src='/show' />");
response.end();
});}
在网上找了许多方法,都无法实现,大部分说的是,rename及其相关方法,无法移动文件到别的区间。解决方法都是设置uploadDir,我设置了无效呢。
我发现这里的files.upload.path很奇怪,我取到文件名,然后
fs.renameSync('/tmp' + filename, "/tmp/test.png");
结果提示居然和没改的一样,好像传递给renameSync的第一个参数无效似的 附错误提示:请高手支招
fs.js:439 return binding.rename(pathModule._makeLong(oldPath), ^ Error: ENOENT, no such file or directory ‘F:\cygwin\tmp\befd1e150d5d8451478fcef24f0d657b’ at Object.fs.renameSync (fs.js:439:18) at F:\cygwin\home\Arrowing\requestHandlers.js:61:6 at IncomingForm.parse (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:121:9) at IncomingForm.EventEmitter.emit (events.js:85:17) at IncomingForm._maybeEnd (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:383:8) at IncomingForm.handlePart (F:\cygwin\home\Arrowing\node_modules\formidable\lib\incoming_form.js:212:12) at File.end (F:\cygwin\home\Arrowing\node_modules\formidable\lib\file.js:71:5) at WriteStream.flush (fs.js:1509:9) at Object.oncomplete (fs.js:297:15) at process.startup.processMakeCallback.process._makeCallback (node.js:238:20)
没人哦~自己解决了,附解决代码
function upload(response, request){
var form = new formidable.IncomingForm();
form.uploadDir = "upload"; // project dir
form.parse(request, function(error, fields, files) {
fs.renameSync(files.upload.path, "./" + form.uploadDir + "/test.png");
response.writeHead(200, {"Content-Type": "text/html"});
console.log(files.upload.path);
response.write("<img src='/show' />");
response.end();
});}
form.parse(request, function(error, fields, files) {
// 这里要先判断一下error,否则后面有可能产生“莫名其妙”的错误
if (error) throw error;
// ...
});
@leizongmin 谢谢,不知道为什么,之前做了OK,最近几天重新测试,request的data事件一直在接收数据,end了之后,form.parse一直没执行回调函数。只好把data事件去掉才行,你知道是什么问题吗?
var http = require(‘http’), url = require(‘url’); function start(route, handle){ function onRequest(request, response){ var url_obj = url.parse(request.url); var pathname = url_obj.pathname, getData = url_obj.query, postData = '’;
/*
request.setEncoding("utf8");
request.addListener('data', function(postDataChunk){
postData += postDataChunk;
});
request.addListener('end', function(){
route(handle, pathname, response, request, postData, getData);
});
*/
route(handle, pathname, response, request, postData, getData);
}
http.createServer(onRequest).listen(80);
console.log('Server has started.');
} exports.start = start;
这个是服务器入口代码,就是添加了注释了那块代码就有问题 如果那里在控制台输入循环取得的数据,控制台就会假死,一直获取不完数据一样 这样: request.addListener('data’, function(postDataChunk){ postData += postDataChunk; console.log(postData); });
然后上传那边解析请求的时候,也一直未完成,没能触发回调函数 form.parse(request, function(error, fields, files) { //alert(‘没进来’); });} 第一次用不会这样的,最近用就会了,不知道是什么原因
@arrowing 去掉这句request.setEncoding("utf8");
就可以了。 你这个例子是从这里抄的吧?http://www.nodebeginner.org/index-zh-cn.html
真是坑人不浅