使用QNetworkAccessManager上传文件错误



我正试图在CentOS 6.4上的Qt 5.0中使用QNetworkAccessManager上传文件到服务器。

我试着在网上遵循一些例子,但没有一个工作。QFTP工作得很好,但速度很慢,现在已弃用。我的上传代码是:

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();
}

这是我的程序的输出:

Completed: 0/0 0% 
Finished   
"Cannot open ftp://username:password@example.com/: is a directory"  
Error code: 202

看一下文档,202意味着:

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

有什么建议吗?

变化:

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

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

有必要指出指向文件的链接

相关内容

  • 没有找到相关文章

最新更新