Nodejs:Windows机器上的Writestream

出于某种原因,当我尝试写我的本地主机上的文件(Windows 7)写入stream将不会打开。 在Linux机器上,它工作正常。 有什么types的权限,我需要在Windows中添加?

我已经以pipe理员身份运行。

这是目前的方法。

// Mainfunction to recieve and process the file upload data asynchronously var uploadFile = function(req, targetdir,callback) { var total_uploaded = 0 ,total_file; // Moves the uploaded file from temp directory to it's destination // and calls the callback with the JSON-data that could be returned. var moveToDestination = function(sourcefile, targetfile) { moveFile(sourcefile, targetfile, function(err) { if(!err) callback({success: true}); else callback({success: false, error: err}); }); }; // Direct async xhr stream data upload, yeah baby. if(req.xhr) { var fname = req.header('x-file-name'); // Be sure you can write to '/tmp/' var tmpfile = '/tmp/'+uuid.v1(); total_file = req.header('content-length'); // Open a temporary writestream var ws = fs.createWriteStream(tmpfile); ws.on('error', function(err) { console.log("uploadFile() - req.xhr - could not open writestream."); callback({success: false, error: "Sorry, could not open writestream."}); }); ws.on('close', function(err) { moveToDestination(tmpfile, targetdir+fname); }); // Writing filedata into writestream req.on('data', function(data,t,s) { ws.write(data,'binary',function(r,e){ total_uploaded = total_uploaded+e; var feed = {user:'hitesh',file:fname,progress:(total_uploaded/total_file)*100}; require('./../../redis').broadCast(JSON.stringify(feed)) }); }); req.on('end', function() { ws.end(); }); } // Old form-based upload else { moveToDestination(req.files.qqfile.path, targetdir+req.files.qqfile.name); } }; 

由于你的代码在Linux上运行良好,它必须是Windows特有的。

 var tmpfile = '/tmp/'+uuid.v1(); 

可能是你的问题。 窗口上的文件夹/path结构是不同的。 尝试使用path模块并更改您的代码

 var path = require('path'); var tmpfile = path.join('tmp', uuid.v1()); 

这同样适用于您的参数targetdir

看到这个相关的问题。

问题是与目录。 除非你有一个C:\ tmp目录(假设你正在从C盘运行节点),它没有任何地方写tmp文件。

您可以创build一个C:\ tmp目录或修改该行

 var tmpfile = '/tmp/'+uuid.v1(); 

类似的东西

 var tmpfile = __dirname + '/tmp/'+ uuid.v1(); 

注意:需要一个类似于C:\ mynodeproject \ tmp的目录