Qt5.4 upload file to ftp?



  • hi all:
    i want up some file to my ftp server,but i don't know how it works ,pls help me,thanks



  • Qt has (maybe had?) a class called Qftp. I read that it was dropped when Qt5 was released but I also saw a post that contradicts this.

    I wrote a project in Qt4 using this. It worked quite well

    The connection / disconnection button slot looked like this:

    @
    if(d_Ftp)
    {
    d_Ftp->abort();

    disconnect(d_Ftp, SIGNAL(stateChanged(int)),this,SLOT(FtpStateChanged(int)));
    disconnect(d_Ftp, SIGNAL(commandFinished(int, bool)),this, SLOT(FtpCommandCompleted(int, bool)));
    disconnect(d_Ftp, SIGNAL(listInfo(const QUrlInfo &)),this, SLOT(UrlList(const QUrlInfo &)));
    disconnect(d_Ftp, SIGNAL(dataTransferProgress(qint64, qint64)),this, SLOT(UpdateProgress(qint64, qint64)));

    d_Ftp->deleteLater();
    d_Ftp = 0;

    this->AppendConnectionLog("Connection Closed");

    d_ConnectButton->setText("Connect");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    return;
    }
    else
    {
    d_Ftp = new QFtp(this);

    this->ClearConnectionLog();

    connect(d_Ftp, SIGNAL(stateChanged(int)),this,SLOT(FtpStateChanged(int)));
    connect(d_Ftp, SIGNAL(commandFinished(int, bool)),this, SLOT(FtpCommandCompleted(int, bool)));
    connect(d_Ftp, SIGNAL(listInfo(const QUrlInfo &)),this, SLOT(UrlList(const QUrlInfo &)));
    connect(d_Ftp, SIGNAL(dataTransferProgress(qint64, qint64)),this, SLOT(UpdateProgress(qint64, qint64)));
    }

    d_Ftp->connectToHost("ftp.server.com", 21);
    d_Ftp->login("user name","password");
    

    @

    Connection State:
    @
    void TServiceSyncDialog::FtpStateChanged(
    int State)
    {
    switch(State)
    {
    case QFtp::LoggedIn:
    this->AppendConnectionLog("Login Complete");
    break;

    case QFtp::Unconnected:
    this->AppendConnectionLog("Unconnected");
    d_ConnectButton->setText("Connect");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    break;

    case QFtp::HostLookup:
    this->AppendConnectionLog("Host Lookup");
    break;

    case QFtp::Connecting:
    this->AppendConnectionLog("Connecting...");
    break;

    case QFtp::Connected:
    this->AppendConnectionLog("Connected to the Server");
    d_ConnectButton->setText("Disconnect");
    d_SyncButton->setEnabled(true);
    d_ReportButton->setEnabled(false);
    d_UploadRepositoryButton->setEnabled(true);
    break;

    case QFtp::Closing:
    this->AppendConnectionLog("Closing...");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    break;
    }
    }
    @

    And various commands
    @
    d_Ftp->cd(d_CurrentPath);
    d_Ftp->get(FtpPath + '/' + FileName, d_File);
    d_Ftp->put(d_File,FtpPath + FileName);
    @

    I found it was important verify commands were executed properly before moving on to the next. My setup was elaborate in this way (the program synchronized a directory to or from an FTP site). I created a command queue that was emptied synchronized with the signals returned.



  • hi Rondog,thank you answer my question,qt5 removed the QFtp class
    [quote author="Rondog" date="1423238641"]Qt has (maybe had?) a class called Qftp. I read that it was dropped when Qt5 was released but I also saw a post that contradicts this.

    I wrote a project in Qt4 using this. It worked quite well

    The connection / disconnection button slot looked like this:

    @
    if(d_Ftp)
    {
    d_Ftp->abort();

    disconnect(d_Ftp, SIGNAL(stateChanged(int)),this,SLOT(FtpStateChanged(int)));
    disconnect(d_Ftp, SIGNAL(commandFinished(int, bool)),this, SLOT(FtpCommandCompleted(int, bool)));
    disconnect(d_Ftp, SIGNAL(listInfo(const QUrlInfo &)),this, SLOT(UrlList(const QUrlInfo &)));
    disconnect(d_Ftp, SIGNAL(dataTransferProgress(qint64, qint64)),this, SLOT(UpdateProgress(qint64, qint64)));

    d_Ftp->deleteLater();
    d_Ftp = 0;

    this->AppendConnectionLog("Connection Closed");

    d_ConnectButton->setText("Connect");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    return;
    }
    else
    {
    d_Ftp = new QFtp(this);

    this->ClearConnectionLog();

    connect(d_Ftp, SIGNAL(stateChanged(int)),this,SLOT(FtpStateChanged(int)));
    connect(d_Ftp, SIGNAL(commandFinished(int, bool)),this, SLOT(FtpCommandCompleted(int, bool)));
    connect(d_Ftp, SIGNAL(listInfo(const QUrlInfo &)),this, SLOT(UrlList(const QUrlInfo &)));
    connect(d_Ftp, SIGNAL(dataTransferProgress(qint64, qint64)),this, SLOT(UpdateProgress(qint64, qint64)));
    }

    d_Ftp->connectToHost("ftp.server.com", 21);
    d_Ftp->login("user name","password");
    

    @

    Connection State:
    @
    void TServiceSyncDialog::FtpStateChanged(
    int State)
    {
    switch(State)
    {
    case QFtp::LoggedIn:
    this->AppendConnectionLog("Login Complete");
    break;

    case QFtp::Unconnected:
    this->AppendConnectionLog("Unconnected");
    d_ConnectButton->setText("Connect");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    break;

    case QFtp::HostLookup:
    this->AppendConnectionLog("Host Lookup");
    break;

    case QFtp::Connecting:
    this->AppendConnectionLog("Connecting...");
    break;

    case QFtp::Connected:
    this->AppendConnectionLog("Connected to the Server");
    d_ConnectButton->setText("Disconnect");
    d_SyncButton->setEnabled(true);
    d_ReportButton->setEnabled(false);
    d_UploadRepositoryButton->setEnabled(true);
    break;

    case QFtp::Closing:
    this->AppendConnectionLog("Closing...");
    d_SyncButton->setEnabled(false);
    d_ReportButton->setEnabled(true);
    d_UploadRepositoryButton->setEnabled(false);
    break;
    }
    }
    @

    And various commands
    @
    d_Ftp->cd(d_CurrentPath);
    d_Ftp->get(FtpPath + '/' + FileName, d_File);
    d_Ftp->put(d_File,FtpPath + FileName);
    @

    I found it was important verify commands were executed properly before moving on to the next. My setup was elaborate in this way (the program synchronized a directory to or from an FTP site). I created a command queue that was emptied synchronized with the signals returned.[/quote]



  • Apparently you can download it as a separate module

    "QFtp":https://qt.gitorious.org/qt/qtftp/source/75b21b033f413b4a32cc7e0cd6b780f59beadf18:

    Another option might be QNetworkAccessManager. I haven't looked at this yet so I am not sure if this is an option.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.