0

I have made a chatroom where users can messages to each other but I want to add a upload file feature, I have found how to do this:

import tkinter as tk
from tkinter import filedialog

def UploadAction(event=None):
    filename = filedialog.askopenfilename()
    print('Selected:', filename)

root = tk.Tk()
button = tk.Button(root, text='Open', command=UploadAction)
button.pack()

root.mainloop()

but I do not know how to send the file to the other users so that they can download it

so could someone please help me

  • Please elaborate on _how_ your application communicate over the net. Usually files are transferred via HTTP using a remote server that accepts the file and make it available for the other user to download. So, the answer pretty much depends on how you implemented stuff.. Your little peace of code does not illustrate anything except file open dialog in Tkinter. – mr_mo Mar 22 '19 at 18:59
  • @mr_mo it is a peer to peer chat room that uses sockets and a tkinter GUI – tkinter786 Mar 22 '19 at 19:01
  • So you can easily stream the bytes array of that file. use `open(file, 'rb')` and start streaming with your socket. – mr_mo Mar 22 '19 at 19:27
  • Possible duplicate of https://stackoverflow.com/questions/9382045/send-a-file-through-sockets-in-python – mr_mo Mar 22 '19 at 19:28
  • @mr_mo how would I intergrate it would the code in my question? – Hass786123 Mar 22 '19 at 19:32

1 Answers1

0

Although this subject is out of topic (this question is actually about sockets and not tkinter), not asked correctly (see how to ask), and possibly a duplicate of Send a file through sockets in Python , here is a short description of what you should have done for the sake of others.

Generally speaking, transferring files is a broad subject to deal with, from compression, file transfer, checksums, etc. We'll concentrate on how to transfer data in general between two computers using sockets because I see that this is your intention.

Depending on your implementation, you should send a request for the other user for a file transfer. Then, if accepted (acceptance/rejection should be a response from the other user), start transferring the file. In the first step, just send an header that describes the file transfer (file name, size, etc.) Then, when accepted, you send a bytes stream of the content. You can read files as bytes with open(file,'rb') and open(file,'wb') for read/write, respectively. Its a good practice to communicate with headers for metadata, check availability, manage balance, etc.

Note that using socket is low-level interface for data transfer. I wouldn't reinvent; I would learn by examples. See Sending a file over TCP sockets in Python post that shown a nice concise code that transfer files. The solution for his problem (termination of a transfer) is given in the answers of that post, but the code is generally OK. Few more minutes on Google search will result with explained tutorials.

Have the best luck with you application!

mr_mo
  • 1,401
  • 6
  • 10