FTP put qnam problem QT5 [solved]



  • Hi are there any working examples for a simple FTP PUT under QT5 (without using the now deprecated QFtp)?

    I used the below code (scraped from this forum and others) but keep on getting a 201 error code (doco says: QNetworkReply::ContentAccessDenied - the access to the remote content was denied (similar to HTTP error 401)).

    The credentials are definitely correct and my ftp server (proftpd) works fine with other clients (eg. cmd line and filezilla
    @
    #include <QCoreApplication>
    #include <QFile>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QTimer>
    #include <QUrl>
    #include <QDebug>

    #include <stdio.h>

    class Uploader: public QObject
    {
    Q_OBJECT
    QNetworkAccessManager manager;
    QFile *data;
    QNetworkReply *reply;

    public:
    Uploader();
    void doUpload(const QString &file);

    public slots:
    void execute();
    void uploadFinished(QNetworkReply *reply);
    void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
    };

    Uploader::Uploader()
    {
    connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(uploadFinished(QNetworkReply*)));
    }

    void Uploader::doUpload(const QString &file)
    {
    QUrl url("ftp://myserver/mydirectory");
    url.setUserName("username");
    url.setPassword("password");

    data = new QFile(file, this);

    if (data->open(QIODevice::ReadOnly)) {
    reply = manager.put(QNetworkRequest(url), data);

    connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress(qint64, qint64)));
    

    }
    else
    qDebug() << "Problem";
    }

    void Uploader::uploadFinished(QNetworkReply *reply)
    {
    qDebug() << "Finished" << reply->error();

    data->deleteLater();
    reply->deleteLater();

    QCoreApplication::instance()->quit();
    }

    void Uploader::execute()
    {
    doUpload("/home/username/myfile.bin");
    }

    void Uploader::uploadProgress(qint64 bytesSent, qint64 bytesTotal) {
    qDebug() << "Uploaded " << bytesSent << "of " << bytesTotal;
    }

    int main(int argc, char **argv)
    {
    QCoreApplication app(argc, argv);

    Uploader uploader;
    QTimer::singleShot(0, &uploader, SLOT(execute()));

    return app.exec();
    }

    #include "main.moc"
    @

    output:
    @
    Uploaded 0 of 0
    Finished 201
    @



  • Ah i have self-answered! It seems i need to specify the filename to write as part of the URL:

    @
    QUrl url("ftp://myserver/mydirectory/filename_to_write.blah");
    @

    Makes sense.


Log in to reply
 

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