gpt4 book ai didi

qt - QNetworkAccessManager 文件上传错误

转载 作者:行者123 更新时间:2023-12-04 12:58:07 26 4
gpt4 key购买 nike

我正在尝试在 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");

有必要指出文件的链接。

关于qt - QNetworkAccessManager 文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16877363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com