I have a server-side program in python that is expecting an image and is working fine when tested with a client-side program in python.
I want to send image to this server using flutter and I'm failing to do so..
Here's my server-side code
import socket #server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET = IP, SOCK_STREAM = TCP
server.bind(('localhost', 1112)) # 127.0.0.1
server.listen()
client_socket, client_address = server.accept()
file = open('2.jpg', "wb")
image_chunk = client_socket.recv(1024) # stream-based protocol
while image_chunk:
file.write(image_chunk)
image_chunk = client_socket.recv(1024)
file.close()
client_socket.close()
I have tried using dio
, http
and MultiPart
Here are snippets from my failed attempts:
MultiPart
var uri = Uri.parse('https://10.0.2.2:1112'); var request = MultipartRequest('POST', uri) ..files.add(await MultipartFile.fromPath( 'picture', filePath, contentType: MediaType('application', 'jpeg'))); var response = await request.send(); if (response.statusCode == 200) print('Uploaded!');
Dio
Dio dio = new Dio(); FormData formData = new FormData.fromMap({ "file": await MultipartFile.fromPath(filePath, filename: basename(filePath), contentType: MediaType('application', 'jpeg'),) }); await dio.post('https://10.0.2.2:1112', data: formData);
I'm able to create a connection but I'm not able to send the file.
P.S: I have almost no experience of working with sockets, so I'm stuck at this.