Qt 6.4.2 and FTP Upload - Windows 11
-
wrote on 16 Jan 2023, 08:53 last edited by Juan de Dironne
I am new to this Forum. I hope my question is well worded and in the right place.
I use this code to test ftp upload with "QNetworkAccessManager" functions.
main.cpp
#include <QApplication> #include <QtWidgets> #include "MyMainWindow.h" int main (int nbArg, char* listArg[]) { QApplication myApp(nbArg,listArg); MyMainWindow myWindow; myWindow.show(); myApp.exec(); }
MyMainWindow.h
#ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include<QtWidgets> #include<QUrl> #include<QFile> #include<QtNetwork/QNetworkAccessManager> #include<QtNetwork/QNetworkReply> class MyMainWindow : public QMainWindow { Q_OBJECT public: MyMainWindow(); public slots: void upload(); void transfertFinished(QNetworkReply*); void displayError(QNetworkReply::NetworkError); private: QAction *m_actionUpload; QAction *m_actionQuit; private: QNetworkAccessManager *m_accesManager; QFile *m_fileTransfert; QNetworkReply *m_responseTransfert; }; #endif // MYMAINWINDOW_H
MyMainWindow.cpp
#include "MyMainWindow.h" MyMainWindow::MyMainWindow() { m_accesManager = new QNetworkAccessManager(this); // Define Actions m_actionUpload = new QAction("Upload",this); m_actionQuit = new QAction("Quit",this); // Define Menu QMenu *fileMenu = menuBar()->addMenu("File"); fileMenu->addAction(m_actionUpload); fileMenu->addAction(m_actionQuit); // Create Slot for Menu connect(m_actionUpload,SIGNAL(triggered()),this,SLOT(upload())); connect(m_actionQuit,SIGNAL(triggered()),qApp,SLOT(quit())); } void MyMainWindow::upload() { // Initialization QString localFileName = "local_file.txt"; QString distantFileName = "distant_file.txt"; QUrl myUrl; QByteArray fileContent; // FTP Parameters myUrl.setScheme("ftp"); myUrl.setHost("myServer.fr"); myUrl.setPort(21); myUrl.setUserName("myLogin"); myUrl.setPassword("myPassword"); myUrl.setPath(distantFileName); // File Object m_fileTransfert = new QFile(localFileName,this); // If File Exist if(m_fileTransfert->exists()) { // Create Request "Object" (from "object" URL) QNetworkRequest myRequest(myUrl); // Open Local File m_fileTransfert->open(QIODevice::ReadOnly); fileContent = m_fileTransfert->readAll(); // Send the File via the Request object m_responseTransfert = m_accesManager->put(myRequest,fileContent); } else qDebug() << "File No Exist"; }
When I test for the first time, I have this error "qt.tlsbackend.ossl: Failed to load libssl/libcrypto"
So I use "Qt Maintenance Tool" to install "OpenSSL 1.1.1q Toolkit".
I then copy the installed "libcrypto-1_1-x64.dll" and "libssl-1_1-x64.dll" files into my executable folder.
And testing the script again no more errors.
But so far no file deposited on my server.And adding error handling with the code below
// Send the File via the Request object m_responseTransfert = m_accesManager->put(myRequest,fileContent); // Slots connect(m_responseTransfert,SIGNAL(errorOccurred(QNetworkReply::NetworkError)),this,SLOT(displayError(QNetworkReply::NetworkError)));
and
void MyMainWindow::displayError(QNetworkReply::NetworkError errorFonc) { qDebug() << "Error : " << errorFonc; qDebug() << m_responseTransfert->errorString(); }
I obtain this error message
Error : QNetworkReply::ProtocolUnknownError
"Protocol "ftp" is unknown"What is wrong or missing in this code to get this error....?
And as specified in the title I am under Windows 11 (this information is important in my opinion)
And I am using "MSVC2019_64bit" compiler -
Hi
I have a feeling maybe its not included or something like that. code looks good.What do you get if you do
qDebug() << m_accesManager->supportedSchemes(); -
wrote on 16 Jan 2023, 21:01 last edited by
Hello!
It seems
Qt 6
does not haveFTP
support inQt Network
because they moved it to some plugin. More details you can get here: https://www.qt.io/blog/qt-network-in-qt-6
Also, I recommend you to check out thisQt SCXML FTP Client Example
: https://doc-snapshots.qt.io/qt6-dev/qtscxml-ftpclient-example.html usingQt 6
. It could be useful for you. -
Hi
I have a feeling maybe its not included or something like that. code looks good.What do you get if you do
qDebug() << m_accesManager->supportedSchemes();wrote on 17 Jan 2023, 08:22 last edited by@mrjj Thank you for taking the time to look and respond to me. I tried your code to see what schemes were supported and... FTP is not include :(
So I must use QT 5 to do FTP...? -
Hello!
It seems
Qt 6
does not haveFTP
support inQt Network
because they moved it to some plugin. More details you can get here: https://www.qt.io/blog/qt-network-in-qt-6
Also, I recommend you to check out thisQt SCXML FTP Client Example
: https://doc-snapshots.qt.io/qt6-dev/qtscxml-ftpclient-example.html usingQt 6
. It could be useful for you.wrote on 17 Jan 2023, 08:25 last edited by@Cobra91151 I had seen this code before but I found this code complex.
So I searched for a code that seemed simpler to me and I came across this solution using "QNetworkAccessManager".
But it seems it doesn't work as you say with QT 6 :( -
wrote on 17 Jan 2023, 08:44 last edited by Bonnie
According to this post https://forum.qt.io/topic/136161/qt6-qnetworkrequest-and-ftp, they planned to move ftp support to plugin in Qt6 but never really done that because "little-to-no demand" and using
libcurl
is an alternative solution. -
I am new to this Forum. I hope my question is well worded and in the right place.
I use this code to test ftp upload with "QNetworkAccessManager" functions.
main.cpp
#include <QApplication> #include <QtWidgets> #include "MyMainWindow.h" int main (int nbArg, char* listArg[]) { QApplication myApp(nbArg,listArg); MyMainWindow myWindow; myWindow.show(); myApp.exec(); }
MyMainWindow.h
#ifndef MYMAINWINDOW_H #define MYMAINWINDOW_H #include<QtWidgets> #include<QUrl> #include<QFile> #include<QtNetwork/QNetworkAccessManager> #include<QtNetwork/QNetworkReply> class MyMainWindow : public QMainWindow { Q_OBJECT public: MyMainWindow(); public slots: void upload(); void transfertFinished(QNetworkReply*); void displayError(QNetworkReply::NetworkError); private: QAction *m_actionUpload; QAction *m_actionQuit; private: QNetworkAccessManager *m_accesManager; QFile *m_fileTransfert; QNetworkReply *m_responseTransfert; }; #endif // MYMAINWINDOW_H
MyMainWindow.cpp
#include "MyMainWindow.h" MyMainWindow::MyMainWindow() { m_accesManager = new QNetworkAccessManager(this); // Define Actions m_actionUpload = new QAction("Upload",this); m_actionQuit = new QAction("Quit",this); // Define Menu QMenu *fileMenu = menuBar()->addMenu("File"); fileMenu->addAction(m_actionUpload); fileMenu->addAction(m_actionQuit); // Create Slot for Menu connect(m_actionUpload,SIGNAL(triggered()),this,SLOT(upload())); connect(m_actionQuit,SIGNAL(triggered()),qApp,SLOT(quit())); } void MyMainWindow::upload() { // Initialization QString localFileName = "local_file.txt"; QString distantFileName = "distant_file.txt"; QUrl myUrl; QByteArray fileContent; // FTP Parameters myUrl.setScheme("ftp"); myUrl.setHost("myServer.fr"); myUrl.setPort(21); myUrl.setUserName("myLogin"); myUrl.setPassword("myPassword"); myUrl.setPath(distantFileName); // File Object m_fileTransfert = new QFile(localFileName,this); // If File Exist if(m_fileTransfert->exists()) { // Create Request "Object" (from "object" URL) QNetworkRequest myRequest(myUrl); // Open Local File m_fileTransfert->open(QIODevice::ReadOnly); fileContent = m_fileTransfert->readAll(); // Send the File via the Request object m_responseTransfert = m_accesManager->put(myRequest,fileContent); } else qDebug() << "File No Exist"; }
When I test for the first time, I have this error "qt.tlsbackend.ossl: Failed to load libssl/libcrypto"
So I use "Qt Maintenance Tool" to install "OpenSSL 1.1.1q Toolkit".
I then copy the installed "libcrypto-1_1-x64.dll" and "libssl-1_1-x64.dll" files into my executable folder.
And testing the script again no more errors.
But so far no file deposited on my server.And adding error handling with the code below
// Send the File via the Request object m_responseTransfert = m_accesManager->put(myRequest,fileContent); // Slots connect(m_responseTransfert,SIGNAL(errorOccurred(QNetworkReply::NetworkError)),this,SLOT(displayError(QNetworkReply::NetworkError)));
and
void MyMainWindow::displayError(QNetworkReply::NetworkError errorFonc) { qDebug() << "Error : " << errorFonc; qDebug() << m_responseTransfert->errorString(); }
I obtain this error message
Error : QNetworkReply::ProtocolUnknownError
"Protocol "ftp" is unknown"What is wrong or missing in this code to get this error....?
And as specified in the title I am under Windows 11 (this information is important in my opinion)
And I am using "MSVC2019_64bit" compilerwrote on 17 Jan 2023, 08:57 last edited by Jonas KvingeSo I use "Qt Maintenance Tool" to install "OpenSSL 1.1.1q Toolkit".
I then copy the installed "libcrypto-1_1-x64.dll" and "libssl-1_1-x64.dll" files into my executable folder.You need one of the TLS backends too, either qschannelbackend.dll or qopensslbackend.dll copied over to a directory called "tls". You only need libcrypto and libssl if you use the openssl backend instead of the schannel.
-
So I use "Qt Maintenance Tool" to install "OpenSSL 1.1.1q Toolkit".
I then copy the installed "libcrypto-1_1-x64.dll" and "libssl-1_1-x64.dll" files into my executable folder.You need one of the TLS backends too, either qschannelbackend.dll or qopensslbackend.dll copied over to a directory called "tls". You only need libcrypto and libssl if you use the openssl backend instead of the schannel.
wrote on 17 Jan 2023, 09:22 last edited by@Jonas-Kvinge The problem was indeed not related to OpenSSL but to the version of QT.
With QT 5.15.2, the code allows to send the file to the Server.
With QT 6.4.2, the code returns an error "Protocol FTP is unknow".But in which case I have to copy the TLS backends...? And the TLS directory you're talking about, where should it be...?
-
@Jonas-Kvinge The problem was indeed not related to OpenSSL but to the version of QT.
With QT 5.15.2, the code allows to send the file to the Server.
With QT 6.4.2, the code returns an error "Protocol FTP is unknow".But in which case I have to copy the TLS backends...? And the TLS directory you're talking about, where should it be...?
wrote on 19 Mar 2024, 11:13 last edited by@Juan-de-Dironne I had similar problems with FTP.
Qt 5 and FTP worked fine
With QT 6, I had to use ScXML to get my app to access FTP site.
It is possible but a lot of work is needed to get scxml to to access the FTP site of choosing