问题描述
我正在寻找有关如何在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\r\n");
totalBytes.append("Content-Disposition: form-data; name=\"email\"\r\n");
totalBytes.append("\r\n");
totalBytes.append("[email protected]\r\n");
totalBytes.append("--AaB03x\r\n");
totalBytes.append("Content-Disposition: form-data; name=\"photo\"; filename=\"" + bytes+ "\"\r\n");
totalBytes.append("Content-Transfer-Encoding: binary\r\n\r\n");
totalBytes.append(file->readAll());
totalBytes.append("\r\n");
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的上载器/下载器应用程序,内部具有由QNetworkAccessManager管理的全套POST标头,因此对于初学者来说将非常有帮助.
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.
所有者: hard.ru
Owner: stiff.ru
提交人: 霍克斯诺克斯
Committer: hoxnox
这篇关于在Qt4上使用POST方法上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!