gpt4 book ai didi

flutter - 如何使用 flutter 将文件发送到 node.js 服务器?

转载 作者:行者123 更新时间:2023-12-03 03:22:40 28 4
gpt4 key购买 nike

我正在使用 socket_io_client包与 nodejs 套接字服务器通信。
这是我的 flutter 套接字配置:

socket = IO.io(SocketUrl.url, <String, dynamic>{
'transports': ["websocket"],
"autoConnect": false
});
socket.connect();
socket.on('connect', (_) {
print('socket connected to server');
});
socket.on('newMessage', (message) {
print(message);
});
socket.on('event', (data) => print(data));
socket.on('disconnect', (_) => print('disconnect'));
}


这是文件发送的逻辑:
const data = {
userId: currentUser.id,
file: file.name
};
const stream = ss.createStream();
ss(socket).emit("send-file", stream, data);
ss.createBlobReadStream(file).pipe(stream);

这就是从节点客户端完成的方式。我需要编写上述代码的 Dart 版本。我看到 quiver 包具有创建流缓冲区的异步函数,但我不知道如何为这个特定的应用程序实现它。

编辑:
 void onSendFile(file, data) async {
final isFile = File(file);
var buffer = isFile.openRead();
//socket.emit("send-file",[buffer,data]);
socket.emitWithBinary("send-file", [buffer, data]);
}

最佳答案

socket_io_client 上阅读 API 引用和问题的 repo,您​​应该能够发送带有 socket.emitWithBinary 的二进制文件.

假设您已经有办法上 File ,你最有可能做到这一点

final myFile = File('file.txt');
final bytes = await myFile.readAsBytes();

socket.emitWithBinary('send-file', bytes);

更新

来自 JS 版本的 socket-io 客户端, emit函数可以接受多个参数,最后是 converted to an array ,所以我假设您可以通过将数组传递给 emitWithBinary 来实现相同的目的。或 emit
备注

看到你的 JS 版本不包含任何 binary标志,您可能想尝试 emit反而。

socket.emit(
'send-file',
[
123,
{
'userId': currentUser.id,
'file': file.name,
},
],
);

关于flutter - 如何使用 flutter 将文件发送到 node.js 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61528618/

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