gpt4 book ai didi

javascript - 如何解决流在 binary.js(BinaryStream.write)的 Node 服务器上不可写?

转载 作者:行者123 更新时间:2023-11-30 21:09:15 24 4
gpt4 key购买 nike

我在文件上传 Node 服务器上工作,上传后一个大文件出现以下错误

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 node version v0.10.48

Reference code picked from https://github.com/rajkissu/binaryjs-upload-stream

我找不到解决这个问题的方法。

问题是 1% 的情况下,代码转到 console.log('Debug 4) 情况并在执行 stream.write({end: true}); 它停止了 Node 服务器,即我得到了错误的网关,因为没有处理抛出的错误。

在前端,我使用来自 Node 服务器的数据读取内容 ID 并设置完成,但由于 Node 服务器停止,我无法从 Node 服务器获取内容 ID。

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});
}
});
}

});
}

最佳答案

我找到了解决您问题的方法,在创建文件对象时使用选项如下

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

关于javascript - 如何解决流在 binary.js(BinaryStream.write)的 Node 服务器上不可写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46302121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com