QT 通过POST方法,上传文件到服务器
来源:互联网 发布:1.76我本沉默复古淘宝 编辑:程序博客网 时间:2024/04/28 04:50
直接上代码:
第一种方法:
QUrl url;
url.setUrl(makeFullUrl(m_opt.submitdebateresult.url));
#if QT_VERSION >= 0x050000
QUrlQuery q;
addSessionIdQueryItem(&q);
q.addQueryItem(m_opt.submitdebateresult.query.mediaId, param.mediaId());
q.addQueryItem(m_opt.submitdebateresult.query.exerciseId, param.exerciseId());
url.setQuery(q);
#else
addSessionIdQueryItem(&url);
url.addQueryItem(m_opt.submitdebateresult.query.mediaId, param.mediaId());
url.addQueryItem(m_opt.submitdebateresult.query.exerciseId, param.exerciseId());
#endif
QNetworkRequest request;
request.setUrl(url);
//这里两个分支分别代码QT5及以主和QT4.8 的的url加参数的方法。
request.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("application/octet-stream"));
// 上面这句,是代码以二进制流的方式post数据。
<span style="font-size:14px;background-color: rgb(255, 255, 255);"><strong> QFile file(filePath); // 这里是你自己的文件路径 file.open(QIODevice::ReadOnly); int file_len = file.size(); QDataStream in(&file); m_buf = new char[file_len]; in.readRawData( m_buf, file_len); file.close();</strong></span>
QByteArray dataArray = QByteArray(m_buf, fileLength);
m_netReply = m_networkAccessManager->post(request , dataArray);
connect(m_netReply, SIGNAL(uploadProgress ( qint64 ,qint64 )), this, SLOT( slotUploadProgress(qint64 ,qint64)));
connect(m_netReply, SIGNAL(finished()), this, SLOT(slotUploadFinished()));
第二种方法:
比第一种简单,但效率没第一种高,只写主要代码。
QFile file(filePath);
file.open(QIODevice::ReadOnly);
data = file.readAll();
file.close();
m_netReply = m_networkAccessManager->post (request, "file="+data.toBase64 ()+"");
// 这里是把文件转成base64的,好处是,防止文件传的过程因为符号,空格的原因,导致文件数据失真。
connect(m_netReply, SIGNAL(uploadProgress ( qint64 ,qint64 )), this, SLOT( slotUploadProgress(qint64 ,qint64)));
connect(m_netReply, SIGNAL(finished()), this, SLOT(slotUploadFinished()));
- QT 通过POST方法,上传文件到服务器
- 将文件通过http的post方法上传到服务器
- Qt Post上传图片文件到服务器
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器.
- 通过 HTTP POST 上传文件到服务器
- python 通过post方式上传文件到php服务器
- android post上传文件到服务器
- NoHttp的post上传文件到服务器
- Java客户端通过Http发送POST请求上传文件到web服务器
- Applet 通过http上传文件到服务器
- android通过servlet上传文件到服务器
- 通过ftp上传文件到指定服务器
- 通过scp上传文件到服务器
- 通过POST请求向服务器上传文件源代码
- Http工具类 同时 post上传 多文件到服务器
- python post方式 上传文件到php服务器
- Android使用post方式上传图片到服务器的方法
- 在iTunes Connect查看崩溃日志
- Session过期
- kernel开机logo总结
- POJ 1035(字符串模拟)
- (一二八)比较成员函数、中括号表示法、静态成员函数
- QT 通过POST方法,上传文件到服务器
- msdn.itellyou.cn
- [Android]自定义圆形图片
- android app 快速接入银联支付流程(android studio版)
- python中函数参数,函数调用参数传递,可变参数,*expression,**expression
- hadoop 2.6.0 安全问题--UserGroupInformation
- 父子页面的相互调用
- kafka基本概念
- JSTL中截取一定长度的字符串长度,如果超过指定长度在未尾加“......”,否则返回原来字符串。