function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
console.log("about to parse");
form.parse(request, function(error, fields, files) {
console.log("parsing done");
console.log("F.U.P: "+files.upload.path);
fs.renameSync(files.upload.path, "/tmp/test.png");
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
});
}
环境:win7、node.exe Ver 0.6.10
是因为权限问题吗?
运行报错:
E:\cygwin\home\dd\dodo>node index.js
F.U.P: C:\Users\dd\AppData\Local\Temp\68ef963377b965f242cf7ae5b4595f31
fs.js:330
return binding.rename(pathModule._makeLong(oldPath),
^
Error: ENOENT, no such file or directory ‘C:\Users\dd\AppData\Local\Temp\68ef9
377b965f242cf7ae5b4595f31’
at Object.renameSync (fs.js:330:18)
at E:\cygwin\home\dd\dodo\requestHandlers.js:35:8
at IncomingForm.<anonymous> (E:\cygwin\home\dd\dodo\node_modules\formidabl
lib\incoming_form.js:117:9)
at IncomingForm.emit (events.js:64:17)
at IncomingForm._maybeEnd (E:\cygwin\home\dd\dodo\node_modules\formidable
b\incoming_form.js:376:8)
at E:\cygwin\home\dd\dodo\node_modules\formidable\lib\incoming_form.js:207
2
at E:\cygwin\home\dd\dodo\node_modules\formidable\lib\file.js:59:5
at Object.oncomplete (fs.js:1289:9)
E:\cygwin\home\dd\dodo>
fs.rename 之前遇到过类似的问题,临时文件写成功了,但是就是无法移动… 猜测是由于磁盘分区导致的,你的例子中也符合这个条件 C: -> E:
改变formidable的默认零时文件夹路径,保证和目标目录处于同一个磁盘分区 formidable.IncomingForm.UPLOAD_DIR = your_path
这样应该ok的
function upload(response, request) {
console.log("Request handler 'upload' was called.");
var form = new formidable.IncomingForm();
form.uploadDir = "tmp"
// console.log("about to parse");
form.parse(request, function(error, fields, files) {
//console.log("parsing done");
console.log("F.U.P: " + files.upload.path);
setTimeout(function(){
try{
fs.renameSync(files.upload.path, "tmp/test.png");
}catch(e){
console.log(e);
}
response.writeHead(200, {"Content-Type": "text/html"});
response.write("received image:<br/>");
response.write("<img src='/show' />");
response.end();
},1000
);
});
}
搞定了。谢谢你~
another reply: https://groups.google.com/group/nodejs/browse_thread/thread/222cb0fd30906d4e
将fs.renameSync(files.upload.path, “/tmp/test.png”);中的"/tmp/test.png"改为 "./tmp/test.png"也是可以的。我就是这么改的。
fs.rename()不能跨磁盘移动文件 renameSync 不能创建新目录 参考 http://stackoverflow.com/questions/10084000/why-use-nodejs-upload-exception
/home/hd-user/github/nodejs-beginner/requestHandlers.js:61
console.log(files.upload.path);
^
TypeError: Cannot read property 'path' of undefined
这个错误大概什么原因呢?
我也照抄的,这里的问题改了下,手动创建了tmp文件。 代码如下:
// 响应upload
var upload = function(response, request) {
console.log('Request handler "upload" was called.');
var form=new formidable.IncomingForm();
console.log('about to parse');
// 写一个临时路径
form.uploadDir='tmp';
form.parse(request,function(err,fields,files){
console.log('parsing done');
console.log(files.upload);
// 同步操作文件,需要try catch
try{
fs.renameSync(files.upload.path,'tmp/test.png');
}catch(e){
console.log(e);
}
response.writeHead(200,{'Content-Type':'text/html'});
response.write('received img:</br>');
response.write('<a href="/show"><img src="/show" /></a>');
response.end();
});
};
// 显示文件
var show=function(response){
console.log('request handler "upload" was called');
fs.readFile('./tmp/test.png','binary',function(err,file){
if(err){
response.writeHead(500,{'Content-Type':'text/plain'});
response.write(err+'\n');
response.end();
}else{
response.writeHead(200,{'Content-Type':'image/png'});
response.write(file,'binary');
response.end();
}
});
};