Pergunta

I have a program in which I have a constant path for an ftp directory. I have a function that needs to access that directory and do some operations on the files within. Before, there was only ever one file in this directory and it always had the same name, so I was told it would be OK to hard-code the path in. In that format, this worked:

void MyClass::FTPReader() { QString filepath = "ftp://user:password@ftp.myftp.com/needed_directory/needed_file.txt"; QNetworkAccessManager *nam = new QNetworkAccessManager(this); QNetworkRequest request(filepath); QNetworkReply *reply = nam->get(request); //Other operations done on data after this... }

However, now it turns out that a) there can be more than one file in this directory, and b) they can have variable names. I do know I will always need all files in the directory, but I don't know the number or the names. Is there any way to loop through the ftp directory (in this case needed_directory and use the network request and reply to get the files individually? I think it probably needs to do the same as above, just without the file-specific part of filename, and then do something with that, but I'm sort of lost how to do that. Thanks!

Foi útil?

Solução

You can use QFtp. For retrieving the list of files in a directory you can use QFtp::list function. When QFtp::list has been called, the listInfo signal is emitted once for each directory entry. It can be done like:

QFtp ftp;
connect( &ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(ftpListInfo(QUrlInfo)) );
ftp.connectToHost( "ftp://user:password@ftp.myftp.com/needed_directory" );

if( ftp.state() == QFtp::LoggedIn )
  ftp.list();

void FtpDialog::ftpListInfo( const QUrlInfo&info )
{
   if( info.isFile() )
      qDebug() << info.name();
}

Outras dicas

You could use the QFtp class as follows:

main.cpp

#include <QFtp>
#include <QDebug>
#include <QCoreApplication>

class MyClass : public QObject
{
    Q_OBJECT
    public:
    MyClass(QObject *parent) : QObject(parent)
    {
        connect(&m_ftp, SIGNAL(listInfo(QUrlInfo)), SLOT(handleListInfo(QUrlInfo)));
        connect(&m_ftp, SIGNAL(stateChanged(int)), SLOT(handleStateChanged(int)));
        m_ftp.connectToHost( "ftp://user:password@ftp.myftp.com/needed_directory" );
        m_ftp.login();
    }

    public slots:
        void handleStateChanged(int state)
        {
            if (state == QFtp::LoggedIn)
                m_ftp.list();
            else
                qDebug() << "Logging in";
        }

        void handleListInfo(const QUrlInfo &info)
        {
           if (info.isFile())
              qDebug() << info.name();
        }

    private:
        QFtp m_ftp;
};

#include "main.moc"

int main(int argc, char **argv)
{
    QCoreApplication coreApplication(argc, argv);
    return coreApplication.exec();
}

main.pro

TEMPLATE = app
TARGET = main
QT = core network
SOURCES += main.cpp

Build and Run

qmake-qt4 && (n)make && ./main
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top