问题描述
我正在寻找有关如何在 Qt 上使用 HTTP POST 方法将文件上传到服务器的基本代码示例.
I'm looking for a basic code samples of how to upload files to server with HTTP POST method on Qt.
我的任务:我有一个简单的 Qt 程序,我需要从本地主机中选择任何图像文件并将其上传到服务器.选择部分和 GUI 很简单,我已经完成了,但是对于 POST 上传我很困惑.另外我要说的是,没有上传文件的权限.
My task: I have simple Qt program and I need to select any image file from the local host and upload it to the server. The selection part and GUI is simple and I have already done it, but with POST uploading I'm confused. In addition I have to say, that there is no authorization to upload file.
如果有人已经在看这个话题?
If someone already looking this topic?
PS:我之所以这么问而不是自己编写代码是因为时间,我需要快速掌握这种方法.
PS: the reason why I'm asking and not coding my self is time, I need to get this method quick.
谢谢,我这边的所有成功解决方案都会在这里发布给其他人.
Thank you, all success solutions from my side will be posted here for others.
添加:这是我的代码,目前还不能运行.上传网站位于这里.
ADDED: Here is my code, that doesn't work yet. Upload site located here.
void CDialog::on_uploadButton_clicked() {
QFileInfo fileInfo(absPathLineEdit->text());
if (!fileInfo.exists()) {
QMessageBox::information(this,
tr("Information"),
tr("File doesn't exists! Please, select another image."));
return;
}
file = new QFile(fileInfo.filePath());
if (!file->open(QIODevice::ReadOnly)) {
QMessageBox::information(this,
tr("Information"),
tr("Unable to open file for reading!"));
return;
}
QString host = "http://data.cod.ru";
QUrl url(host);
QHttp::ConnectionMode mode = QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, (url.port() == -1) ? 80 : url.port());
QHttpRequestHeader header("POST", "/", 1, 1);
header.setValue("Host", "data.cod.ru");
header.setValue("Content-type", "multipart/form-data, boundary=AaB03x");
header.setValue("Cache-Control", "no-cache");
header.setValue("Accept", "*/*");
QByteArray bytes(fileInfo.filePath().toUtf8());
QByteArray totalBytes;
totalBytes.append("--AaB03x
");
totalBytes.append("Content-Disposition: form-data; name="email"
");
totalBytes.append("
");
totalBytes.append("[email protected]
");
totalBytes.append("--AaB03x
");
totalBytes.append("Content-Disposition: form-data; name="photo"; filename="" + bytes+ ""
");
totalBytes.append("Content-Transfer-Encoding: binary
");
totalBytes.append(file->readAll());
totalBytes.append("
");
totalBytes.append("--AaB03x--");
header.setContentLength(totalBytes.length());
httpRequestAborted = false;
httpGetId = http->request(header, totalBytes);
file->close();
}
并阅读下面的回答功能:
and read answer function below:
void CDialog::httpRequestFinished(int requestId, bool error) {
if (requestId != httpGetId)
return;
if (httpRequestAborted) {
if (file) {
file->close();
// file->remove();
// delete file;
file = 0;
}
return;
}
if (requestId != httpGetId)
return;
file->close();
if (error) {
// file->remove();
QMessageBox::information(this, tr("HTTP"),
tr("Download failed: %1.")
.arg(http->errorString()));
} else {
QByteArray data = http->readAll();
QFile *dataFile = new QFile("answer.txt");
dataFile->open(QIODevice::WriteOnly | QIODevice::Text);
dataFile->write(data);
dataFile->flush();
dataFile->close();
}
// delete file;
file = 0;
}
推荐答案
另外,我今天发现了不错的代码:链接文本
Also, I found today nice code: link text
它是基于Qt4的上传/下载应用程序,里面有全套的POST headers由QNetworkAccessManager管理,所以对于初学者来说会很有帮助.
It's uploader/downloader app based on Qt4, with full set of POST headers managed by QNetworkAccessManager inside, so for beginners it will be very helpful.
所有者:僵硬的.ru
提交者:霍克斯诺克斯
这篇关于在 Qt4 上使用 POST 方法上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!