Downloading file from FTP server



  • Hello all , i want to download a file ( more specifically a PDF file) from an FTP server and later on print it . I want to build it for windows 7/8/10

    I did looked around the web and did found some examples but on some it was clearly mentioned that the program is deprecated .

    Where should i look into ?


  • Qt Champions 2017

    Hi
    You can still use the QFtp class or use external lib like CURl.

    If you do not require many ftp functions/integration, you could just use curl or wget command line tool and simply
    fire it via QProcess.



  • i am not sure if curl or wget will work on windows . Moreover i want to distribute this utility program so i cannot be sure that others might also have same environment as mine



  • In order to have an http server service based on Qt I'm using Poco
    https://pocoproject.org/ It is very great!
    It has got an ftp component too.


  • Qt Champions 2017

    @Qjay
    Hi
    Both wget and CURL runs on windows and linux.
    CURL also run macOS. (CURL even runs in DOS ;)
    https://curl.haxx.se/download.html
    https://eternallybored.org/misc/wget/

    You can just include the tools (.exe) in apps folder.
    They are standalone cmd line, requiring no install and will
    only add 10 MB to the installer size.

    Anyway, not knowing what you really need, maybe sing POCO or QFTP/ something else integrated is better.
    Running a cmdline tool with QProcess is just very easy if all you need is to download a predefined file.



  • @mrdebug thanks i will try it out :D



  • Curl has a C API https://curl.haxx.se/libcurl/ with bindings in other languages like C++ (http://www.curlpp.org/) so you can build it and link to it on basically all platforms Qt supports.

    Having said that, if you have an ftp url and just want to download it, QNetworkAccessManager is more than enough to achieve your target


  • Qt Champions 2017

    @VRonin said in Downloading file from FTP server:

    QNetworkAccessManager

    Hi
    I could not find a sample for download but this uploads to ftp
    https://evileg.com/en/post/255/

    Please update if you find downloading from ftp sample :)



  • you can use qftp class derectly in qt4
    if you are using qt5 ,you can get this class from github



  • @mrjj said in Downloading file from FTP server:

    Please update if you find downloading from ftp sample

    It's just a normal get request, as it was http:

    #include <QCoreApplication>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc,argv);
        QNetworkAccessManager netMan;
        QNetworkReply* const repl = netMan.get(QNetworkRequest(QUrl::fromUserInput(R"**(ftp://131.225.104.13/linux/.snapshot/NDMP_AUTO_SNAPSHOT3210/fermi/contrib/obsolete/video/nvidia/5328/NVIDIA.5328.README.txt)**")));
        QObject::connect(repl,&QNetworkReply::readyRead,[repl]()->void{
             qDebug().noquote() << repl->readAll();
         });
        QObject::connect(repl,QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[repl]()->void{
             qDebug() << "Error: " + repl->errorString();
         });
        return a.exec();
    }
    

    @James-Haitching said in Downloading file from FTP server:

    you can get [QFtp] from github

    No, that module is outdated and not maintained. Never use old code when you have a chance of using cryptography. If you want to do more advanced stuff (like getting directory listing) then use libcurl/curlpp



  • thanks for so many solutions . I will try all of that but for the time being ( as it was urgent) i used python ( ftp module) and tkinter for a very quick gui .



  • @VRonin said in Downloading file from FTP server:

    QNetworkAccessManager netMan;
    QNetworkReply* const repl = netMan.get(QNetworkRequest(QUrl::fromUserInput(R"(ftp://131.225.104.13/linux/.snapshot/NDMP_AUTO_SNAPSHOT3210/fermi/contrib/obsolete/video/nvidia/5328/NVIDIA.5328.README.txt)")));
    QObject::connect(repl,&QNetworkReply::readyRead,repl->void{
    qDebug().noquote() << repl->readAll();
    });
    QObject::connect(repl,QOverloadQNetworkReply::NetworkError::of(&QNetworkReply::error),repl->void{
    qDebug() << "Error: " + repl->errorString();
    });

    suppose i have to pass login details too , how can i do it ? FTP server is password protected



  • #include <QCoreApplication>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    #include <QNetworkRequest>
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc,argv);
        QNetworkAccessManager netMan;
        QNetworkReply* const repl = netMan.get(QNetworkRequest(QUrl::fromUserInput(R"**(ftp://131.225.104.13/linux/.snapshot/NDMP_AUTO_SNAPSHOT3210/fermi/contrib/obsolete/video/nvidia/5328/NVIDIA.5328.README.txt)**")));
        QObject::connect(repl,&QNetworkReply::readyRead,[repl]()->void{
             qDebug().noquote() << repl->readAll();
         });
        QObject::connect(repl,QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),[repl]()->void{
             qDebug() << "Error: " + repl->errorString();
         });
        QObject::connect(repl,&QNetworkReply::finished,repl,&QNetworkReply::deleteLater);
        QObject::connect(&netMan,&QNetworkAccessManager::authenticationRequired,repl,[repl](QNetworkReply *reply, QAuthenticator *authenticator)->void{
            if(reply!=repl) 
                return;
            aAuthenticator->setUser("MyUserName");
            aAuthenticator->setPassword("MyPassword");
        });
        return a.exec();
    }
    


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