出于某种原因,当我尝试在localhost(Windows 7)上写入文件时,writestream将无法打开。在linux机器上,它工作正常。我需要在Windows中添加某种类型的权限吗?
我已经以管理员身份运行了。
这是当前的方法。
// 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);
}
};
答案 0 :(得分:2)
由于您的代码在Linux上正常运行,因此它必须是Windows特有的。
var tmpfile = '/tmp/'+uuid.v1();
可能是你的问题。 Windows上的文件夹/路径结构是不同的。尝试使用path
模块并将代码更改为
var path = require('path');
var tmpfile = path.join('tmp', uuid.v1());
您的参数targetdir
可能也是如此。
请参阅this相关问题。
答案 1 :(得分:1)
问题在于目录。除非您有一个C:\ tmp目录(假设您正在从C驱动器运行节点),否则它没有任何地方可以写入tmp文件。
您可以创建C:\ tmp目录或修改行
var tmpfile = '/tmp/'+uuid.v1();
类似
var tmpfile = __dirname + '/tmp/'+ uuid.v1();
注意:需要一个像 C:\ mynodeproject \ tmp
这样的目录