简体   繁体   English

通过socket.io-stream发送电子文件失败,并显示“ TypeError:无效的非字符串/缓冲区块”

[英]Electron sending file by socket.io-stream failed with 'TypeError: Invalid non-string/buffer chunk'

I'm creating an app with Electron. 我正在用Electron创建一个应用程序。 I was need to sent file to server with Socket.IO, so I installed socket.io-stream module. 我需要使用Socket.IO将文件发送到服务器,所以我安装了socket.io-stream模块。 I tested on browser, it works well. 我在浏览器上进行了测试,效果很好。 But do same in Electron, it always fails with 但是在Electron中做同样的事情,它总是会失败

TypeError: Invalid non-string/buffer chunk TypeError:无效的非字符串/缓冲区块

This is server side code: 这是服务器端代码:

ss(socket).on('/user/update/profile', (stream, data) => {
    const filename = path.basename(data.name);
    const ws = fs.createWriteStream(`userdata/profile/${filename}`);

    stream.on('error', (e) => {
        console.log('Error found:');
        console.log(e);
    });
    stream.on('drain', (e) => {
        console.log('drain');
    });
    stream.on('data', () => {
        console.log('data');
    });
    stream.on('close', () => {
        console.log('close');
    });
    stream.pipe(ws);

    //ss(socket).emit('/user/update/profile', {});
});

And this is client side code: 这是客户端代码:

var file = ev.target.files[0];
var stream = ss.createStream();

ss(socket).emit('/user/update/profile', stream, {
    email: this.props.user.email,
    name: file.name,
    size: file.size
});

var blobStream = ss.createBlobReadStream(file);
var size = 0;
blobStream.on('data', (chunk) => {
    size += chunk.length;
    console.log(`${size} / ${file.size}`);
});
blobStream.pipe(stream);

code is quite simple, just from the example in module's introduction page on NPM. 代码很简单,仅来自NPM模块介绍页面中的示例。 I already said that it worked as browser. 我已经说过,它可以用作浏览器。 You see that I logged every file uploading progress on the console. 您会看到我在控制台上记录了每个文件的上传进度。 Using Electron, sending file seems to work because it logged every data size, but on server side, it fails. 使用Electron,发送文件似乎可以正常工作,因为它记录了每种数据大小,但是在服务器端却失败了。

I found similar issue with NW.js and that guy solved his problem with his own way but that didn't worked for me. 我在NW.js中发现了类似的问题,那个家伙用自己的方式解决了他的问题,但这对我没有用。

It will be very appreciate help me how should I do. 非常感谢您帮我该怎么做。

The error means your are passing a non-string (eg an object) where a string is expected. 该错误表示您正在传递非字符串(例如,对象),该字符串应为字符串。 I cannot see where that might be offhand but since it works in the Browser but not Electron it might be that the underlying Javascript engines have a different tolerance for the issue. 我看不到可能会出现问题的地方,但是由于它可以在浏览器中工作,而不能在Electron中工作,因此底层Javascript引擎对此问题的容忍度可能有所不同。 Can you run the same in a node debugger? 可以在节点调试器中运行相同的代码吗? Does one of the streams complain or break oddly? 其中一条流会抱怨还是奇怪地中断? Can you narrow down which line this is coming from? 您能缩小范围吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM