我正在尝试使用 Qt 的 QNetworkAccessManager 类上传文件。这是代码:
void TrainWindow::upload ()
{
QString upload_url;
QString trigger_url;
upload_url.append(hostname);
trigger_url.append(hostname);
upload_url.append("darshan");
data = new QFile("/home/darshan/aindra/1.png", this);
if (data->open(QIODevice::ReadOnly))
{
manager = new QNetworkAccessManager();
reply = manager->post(QNetworkRequest(QUrl(upload_url)), data);
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(requestFinished(QNetworkReply*)));
connect(reply, SIGNAL(uploadProgress(qint64, qint64)), SLOT(uploadProgress(qint64, qint64)));
}
else
{
qDebug() << "Could not open file to FTP";
}
}
void TrainWindow::requestFinished (QNetworkReply *r)
{
qDebug() << "Finished ";
qDebug()<< r->errorString();
qDebug()<<r->attribute( QNetworkRequest::HttpStatusCodeAttribute).toInt();
qDebug()<<r->readAll();
r->deleteLater();
}
void TrainWindow::uploadProgress(qint64 done, qint64 total) {
double percent;
if(done > 0 && total > 0)
{
percent = (done*100)/total;
}
qDebug()<<"Completed: " + QString::number(done) + "/" + QString::number(total) + " " + QString::number(percent) + "%";
}
当我尝试上传时,我收到一条错误消息:
"Unknown error"
301
"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href=(given address)>here</a>.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at (given address) Port 80</address>
</body></html>
我不明白为什么我会收到这个错误。我尝试在 upload_url 路径中包含文件名,以将图像上传到服务器中(这是某些博客中给出的解决方案),但这次我收到了一条不同的错误消息:
"Error downloading (given address) - server replied: Not Found"
404
"<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /darshan/1.png was not found on this server.</p>
<hr>
<address>Apache/2.2.22 (Ubuntu) Server at (given address) Port 80</address>
</body></html>
"
为什么我尝试上传时显示下载错误?