Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. FTP put qnam problem QT5 [solved]
Forum Updated to NodeBB v4.3 + New Features

FTP put qnam problem QT5 [solved]

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 5.7k 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.
  • G Offline
    G Offline
    gurpal2000
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • G Offline
      G Offline
      gurpal2000
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1

      • Login

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