Reputation: 7498
I am trying to send a binary file like image or pdf over a socket. I have tried following:
file.readAsBytesSync().forEach((f) {
socket.write(f);
});
And
socket.write(file.readAsBytesSync());
But I receive a list of integers instead of a binary file. Code given below in JAVA works fine and receiving port gets file in correct format
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
while (fis.read(buffer) > 0) {
dos.write(buffer);
}
fis.close();
dos.close();
I am trying to figure out what would be Dart equivalent of above java code.
Thanks in advance.
Upvotes: 3
Views: 3438
Reputation: 7498
Correct code:
Socket s = await Socket.connect(host, port);
await s.addStream(file.openRead());
print("ok: data written");
Upvotes: 4