For some reason when I try to write a file on my localhost (windows 7) the writestream won't open. On a linux machine, it works fine. Is there some type of permissions I need to add in 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.
var tmpfile = '/tmp/'+uuid.v1();
might be your problem. The folder/path structure on windows is different. Try using the path
module and change your code to
var path = require('path');
var tmpfile = path.join('tmp', uuid.v1());
The same goes probably to your parameter targetdir
.
see this related question.
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.
You could either create a C:\tmp directory or modify the line
var tmpfile = '/tmp/'+uuid.v1();
to something like
var tmpfile = __dirname + '/tmp/'+ uuid.v1();
Note: requires a directory something like C:\mynodeproject\tmp