Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Upload Error With QNetworkAccessManager

I am trying to upload a file to a server using QNetworkAccessManager in Qt 5.0 on CentOS 6.4.

I have tried following a few examples online but none of them work. QFTP works just fine but is slow and now deprecated. My code for the upload is:

void ftp::start(QString fileLocation)
{

    QUrl url2("ftp://example.com");
    url2.setUserName(ftpusername);
    url2.setPassword(ftppassword);

    data = new QFile(fileLocation, this);
    if (data->open(QIODevice::ReadOnly)) {
        nam = new QNetworkAccessManager();
        reply = nam->put(QNetworkRequest(url2), data);

        connect(nam, SIGNAL(finished(QNetworkReply*)),this, SLOT(requestFinished(QNetworkReply*)));
        connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress2(qint64, qint64)));
        connect(reply, SIGNAL(finished()), SLOT(uploadDone()));
    }
    else
    {
        qDebug() << "Could not open file to FTP";
    }
}

void ftp::uploadProgress2(qint64 done, qint64 total) {
    double percent;
    if(done > 0 && total > 0)
    {
        percent = (done*100)/total;
    }
    myParent->addLog("Completed: " + QString::number(done) + "/" + QString::number(total) + " " + QString::number(percent) + "%");
}

void ftp::uploadDone() {
    qDebug() << "Error Code: " << reply->error();
    data->deleteLater();
    reply->deleteLater();
}

void ftp::requestFinished(QNetworkReply* r)
{
    qDebug() << "Finished ";
    qDebug()<< r->errorString();
}

This is the output from my program:

Completed: 0/0 0% 
Finished   
"Cannot open ftp://username:[email protected]/: is a directory"  
Error code: 202

Looking at the docs, 202 means:

QNetworkReply::ContentOperationNotPermittedError The operation requested on the remote content is not permitted

Any suggestions?

like image 927
Brianjs Avatar asked Jun 01 '13 21:06

Brianjs


1 Answers

Change:

QUrl url2("ftp://example.com");

to

QUrl url2("ftp://example.com/somefile");

It is necessary to point out the link to a file.

like image 163
Jean Richard Lima Avatar answered Oct 13 '22 07:10

Jean Richard Lima