在Qt4上使用POST方法上传文件

编程入门 行业动态 更新时间:2024-10-23 09:37:43
本文介绍了在Qt4上使用POST方法上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找有关如何在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 = "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("billgates@microsoft\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方法上传文件

本文发布于:2023-10-11 22:42:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传文件   方法   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!