[英]Nodejs: Writestream on windows machine
For some reason when I try to write a file on my localhost (windows 7) the writestream won't open.出于某种原因,当我尝试在本地主机(Windows 7)上写入文件时,写入流将无法打开。 On a linux machine, it works fine.
在 linux 机器上,它工作正常。 Is there some type of permissions I need to add in windows?
我需要在 Windows 中添加某种类型的权限吗?
I'm already running as administrator.我已经以管理员身份运行了。
Here is the current method.这是当前的方法。
// 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);
}
};
As your code is running fine on Linux it must be something specific to Windows.由于您的代码在 Linux 上运行良好,因此它必须是特定于 Windows 的。
var tmpfile = '/tmp/'+uuid.v1();
might be your problem.可能是你的问题。 The folder/path structure on windows is different.
Windows 上的文件夹/路径结构不同。 Try using the
path
module and change your code to尝试使用
path
模块并将您的代码更改为
var path = require('path');
var tmpfile = path.join('tmp', uuid.v1());
The same goes probably to your parameter targetdir
.您的参数
targetdir
可能targetdir
。
The problem is with the directory.问题出在目录上。 Unless you have a C:\\tmp directory (assuming you're running node from the C drive), it doesn't have anywhere to write the tmp file.
除非您有 C:\\tmp 目录(假设您从 C 驱动器运行 node),否则它没有地方可以写入 tmp 文件。
You could either create a C:\\tmp directory or modify the line您可以创建一个 C:\\tmp 目录或修改该行
var tmpfile = '/tmp/'+uuid.v1();
to something like像
var tmpfile = __dirname + '/tmp/'+ uuid.v1();
Note: requires a directory something like C:\\mynodeproject\\tmp注意:需要一个类似于C:\\mynodeproject\\tmp 的目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.