QNetworkManager uploading file to FTP crash

怎甘沉沦 提交于 2020-01-05 23:18:11

问题


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:


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().



来源:https://stackoverflow.com/questions/20566192/qnetworkmanager-uploading-file-to-ftp-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!