Qt6 FTP
-
How do you FTP get a file with Qt6? I have found a number of questions about this but no real answers.
I'm trying to convert some real old Windows code to Qt. In the Windows code this was relatively easy:
CInternetSession sess(_T("Spooler Session")); CFtpConnection *pConnect = NULL; CInternetFile *file = NULL; // connect to the FTP server at ip:port with userName and userPassword pConnect = sess.GetFtpConnection(ip, userName, userPassword, port, TRUE); // open and read the file file = pConnect->OpenFile(fileName, GENERIC_READ, INTERNET_FLAG_RELOAD | FTP_TRANSFER_TYPE_BINARY); ULONGLONG len = file->GetLength(); CArchive ar_ftp(file, CArchive::load); // read the file using ar_ftp
The CArchive stuff is not necessary in the new Qt code. I just want to connect to the FTP server at ip:port with the userName and userPassword then get the entire fileName from the server and save it to a local file. I can already open and handle that file properly.
I think this would be relatively simple but I can't find examples.
Regards
-
You can essentially follow the pattern in HTTP Example, providing an ftp: scheme URL in QNetworkRequest in place of an http: scheme URL.
-
Qt 6 does not currently support FTP. See https://forum.qt.io/post/726965 for alternatives
-
-
Well this may blow a hole in my conversion to Qt6. Ugh...
Why would they remove something as basic as FTP support? That doesn't make any sense.
Grrr...
-
@bigguiness said in Qt6 FTP:
Why would they remove something as basic as FTP support? That doesn't make any sense.
Because it's deprecated since the early Qt5 times and ftp is security flawed all over?
-
@Christian-Ehrlicher There are still a lot of embedded devices out there that run a FTP server.
They are typically NOT connected to the internet so security should not really be a problem.
For my case, the FTP server isn't even running on the normal FTP port 21. And it requires a username and password. And I don't care that these are passed as clear text. It's an embedded device, it's not connected to the internet, and the user already knows the username and password.
-
@ChrisW67 I was wrong, the HTTP Example is sending the request to my target device. But the device closes the connected instantly, probably due to no username/password. Not sure...
The HTTP Example never shows the dialog to enter the username/password.
-
@bigguiness said in Qt6 FTP:
The HTTP Example never shows the dialog to enter the username/password.
See @JKSH reply in the other FTP discussion; wrapping FTP cURL calls within QProcess may be a good approach to take if you want to use FTP with Qt6. Sometimes old protocols should be left to die (or upgraded as with SFTP).
-
@bigguiness you could use libcurl and this c++ ftp client
-
@bigguiness said in Qt6 FTP:
Well this may blow a hole in my conversion to Qt6
Why? Qt 6 itself doesn't currently contain an FTP class, but it's easy enough to use an alternative with Qt 6. See my link, or @DerReisende's link.
Why would they remove something as basic as FTP support?
Explained at https://forum.qt.io/post/713826
-
This does not exactly do what I want, but...
https://github.com/zycliao/ftp-qt
The ftpClient project in that repo does compile and run with Qt 6.2.1 (after a bit of cleanup).
I just need to figure out a wan to use the client code to do a canned FTP get or put to download or upload a file without any user interaction.
Simplified steps (in my mind) are.
Get:
- create the ftp get object
- connect a signal / slot to let me know when the download is complete
- set the login credentials (ip address, username, password)
- connect to the FTP server
- optionally, check the fetched file list to ensure that the file I want to "Get" exists
- download the file
- the signal / slot then lets me know when the download is complete and the ftp get object can be deleted
Put:
- create the ftp put object
- connect a signal / slot to let me know when the upload is complete
- set the login credentials
- connect to the FTP server
- upload the file
- the signal / slot then lets me know when the upload is complete and the ftp put object can be deleted
Anyone have some suggestions?
-
@bigguiness if you look into
client.cpp
Client::upFile(…)
for your Put operation you will find that the transfer is synchronous and there are no signals emitted. -
@DerReisende, adding a new signal and emitting it at the end of Client::upFile() should work.
The main problem I have is how to replace the ftpClient class with some generic "ftp" class to can the get/put of files.
-
@DerReisende, actually we do get a signal at the end of an upload.
connect(clientThread, SIGNAL(finished()), clientThread, SLOT(stop()));
The upload starts the clientThread with the TUp task. ClientThread::run() then calls client->upFile() which returns once the upload is complete. That causes the thread to end and emit the finished() signal.
Currently ClientThread::stop() outputs a debug message then ensures that the thread is terminated.
-
Well I got the ftp-qt ftpClient code to work the way I need it.
I rewrote the ftpClient class to remove all the ui interaction and changed the base class from QMainWindow to QObject.
I changed all the slot methods to public methods that my wrapper class can then call.
I wrote a ftpSession wrapper class for the ftpClient class. The wrapper class has methods to build a task list. Once the task list is created there is a method to start() running the tasks. As each task is completed by the ClientThread it emits a commandComplete() signal. The ftpSession then executes the next task in the list. The final task causes the ftpSession to emit a finished() signal that my main code uses to know that the session is complete.
It may be ugly but at least it works....