QNetworkManager uploading file to FTP crash

1.8k views Asked by At

I am trying to upload a simple test text file to a FTP server. In order to achieve this I am using QNetworkAccessManager, since QFtp has been deprecated in Qt 5.1.

I created a test.txt file in the programs directory and using QFile I am opening it as QIODevice::ReadWrite | QIODevice::Text.

The problem is when I set the connection and tell the QNetworkAccessManager to upload a file the program crashes ("FTPConnectionTest does not respond"). It happens both when I am trying to use an external FTP server or a local one created with FileZilla.

I connected all signals emitted by the reply (functions: uploadFinish, uploadProgress, uploadError) however no feedback is beeing captured.

Question: Is this problem lying on the side of FTP server or am I doing something wrong in my code? Code snipped below:

Main.cpp

#include <QCoreApplication>
#include <ftp.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    Ftp ftp;
    return a.exec();
}

ftp.cpp

#include "ftp.h"
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QFile>
#include <QUrl>
#include <QDebug>

Ftp::Ftp()
{   
    QFile file("test.txt");
    if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
        url = QUrl("ftp://127.0.0.1/test.txt");
        url.setUserName("user");
        url.setPassword("password");

        qDebug() << "URL set" << url;

        QNetworkAccessManager* nam = new QNetworkAccessManager();
        qDebug() << "nam set";
        QNetworkReply *rep = nam->put(QNetworkRequest(url), &file);
        qDebug() << "after rep";

        connect(rep, SIGNAL(finished()), this, SLOT(uploadFinish()));
        connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(uploadError(QNetworkReply::NetworkError)));
        connect(rep, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64)));
    }
    else qDebug() << "failed to open";
}

void Ftp::uploadFinish()
{
    qDebug() << "finished uploading file";
}

void Ftp::uploadProgress(qint64 a, qint64 b)
{
    qDebug() << a << "/" << b;
}

void Ftp::uploadError(QNetworkReply::NetworkError state)
{
    qDebug() << "State" << state;
}
1

There are 1 answers

1
Pavel Strakhov On BEST ANSWER

See the QNetworkAccessManager::put documentation:

data must be opened for reading when this function is called and must remain valid until the finished() signal is emitted for this reply.

Your file object falls out of scope when the constructor finishes execution, so QNetworkAccessManager probably tries to read from object that is already deleted. You need to make file a class member variable or create it using QFile* file = new QFile().