Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt5.4 upload file to ftp?
Qt 6.12 LTS is out! See what's new in the release blog

Qt5.4 upload file to ftp?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    seamanmei
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rondog
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        seamanmei
        wrote on last edited by
        #3

        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]

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Rondog
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt
          • Unsolved