Reputation: 1086
I am working on file upload node server, for one large file after upload I am getting below error
Error: Stream is not writable
at BinaryStream.write (/node_modules/binaryjs/lib/stream.js:84:11)
at /src/K/Cli/upload/lib/media.js:120:36
at ChildProcess.exithandler (child_process.js:742:7)
at ChildProcess.emit (events.js:110:17)
at maybeClose (child_process.js:1015:16)
at Process.ChildProcess._handle.onexit (child_process.js:1087:5) I am using nodeversion v0.10.48
Reference code picked from https://github.com/rajkissu/binaryjs-upload-stream
I am unable to find the way how to resolve this.
the problem is for 1% case below code goes to console.log('Debug 4)
case and upon executing stream.write({end: true});
it stop node server ie., I get bad gateway, since thrown error is not handled.
and on front end I am using data from node server read content id and set done, but since node server stop and I am not able to get content id from node server.
bs = new BinaryServer({ port: 9004 });
bs.on('connection', function (client) {
client.on('stream', function (stream, meta) {
upload(stream, meta);
});
});
function upload(stream, meta) {
var file = fs.createWriteStream(uploadPath + '/' + meta.name);
stream.pipe(file);
stream.on('end', function () {
// console.log(__dirname);
var cli_base=__dirname+'/../../';
var cmd = '<upload systme cmd>';
console.log('Uploading End and running command ' + cmd);
try {
stream.write({uploaded: true});
}catch (e) {
console.log('Debug 3, Streem On End Event : '+e.message,meta);
// stream.write({end: true});
}
if(!meta.hasOwnProperty('executed')){
meta.executed=true;
child = exec(cmd, function (error, stdout, stderr) {
try{
console.log(error, JSON.parse(stdout), stderr);
var command_output = JSON.parse(stdout);
if(command_output.hasOwnProperty('content_id')){
stream.write({end: true, content_id: command_output.content_id,files:command_output.file});
console.log(command_output.content_id);
}
}catch (e) {
console.log('Debug 4, Streem On End Event : '+e.message,meta,child);
stream.write({end: true});
}
});
}
});
}
Upvotes: 1
Views: 1103
Reputation: 76
I found solution for your problem, use options while creating file object as below
var options = {flags: 'w', encoding: 'utf8',fd: null,mode: '0666'};
var file = fs.createWriteStream(uploadPath + '/' + meta.name,options);
Node reference https://nodejs.org/docs/v0.12.5/api/fs.html#fs_fs_createwritestream_path_options
Encoding options for
createWriteStream
https://nodejs.org/docs/v0.12.5/api/fs.html#fs_fs_writefile_filename_data_options_callback
Upvotes: 1