时间:2021-07-01 10:21:17 帮助过:2人阅读
心酸的过程我就不说了,直接上菜。
如何用Qt实现:curl -F file=@task.plist http://www.fatjb.com/uploadfile
QFile file(m_sTaskPlistPath); if(file.exists()) { if(!file.open(QIODevice::ReadOnly)) { QMessageBox::warning(this, tr("Warning"), tr("打开task.plist失败!"), QMessageBox::Yes); return; } QByteArray fileContent = file.readAll(); file.close(); QString sCrlf="\r\n"; qsrand(QDateTime::currentDateTime().toTime_t()); QString b=QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString(); QString sBoundary="---------------------------"+b; QString sEndBoundary=sCrlf+"--"+sBoundary+"--"+sCrlf; QString sContentType="multipart/form-data; boundary="+sBoundary; sBoundary="--"+sBoundary+sCrlf; QByteArray boundary=sBoundary.toAscii(); QByteArray sendData; sendData.append(boundary); sBoundary = sCrlf + sBoundary; boundary = sBoundary.toAscii(); sendData.append(QString("Content-Disposition: form-data; name=\"file\"; filename=\""+QString(m_sTaskPlistPath.toUtf8().constData())+"\""+sCrlf).toAscii()); sendData.append(QString("Content-Transfer-Encoding: 8bit"+sCrlf).toAscii()); sendData.append(sCrlf.toAscii()); sendData.append(fileContent); sendData.append(sEndBoundary.toAscii()); QNetworkRequest req(QUrl(m_sAddress+m_sUploadPath)); req.setHeader(QNetworkRequest::ContentTypeHeader, sContentType.toAscii()); req.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(sendData.size()).toString()); QNetworkReply* pReply = m_pManager->post(req, sendData); connect(pReply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(NetworkError(QNetworkReply::NetworkError))); } else { QMessageBox::warning(this, tr("Warning"), tr("task.plist不存在!"), QMessageBox::Yes); }
为什么我会知道?下个wireshark抓包,然后跟着正确包慢慢调就知道了,果然还是不能懒。
QNetworkAccessManager实现curl上传表单文件
标签: