发布网友 发布时间:2022-04-23 13:36
共2个回答
懂视网 时间:2022-04-30 09:29
心酸的过程我就不说了,直接上菜。
如何用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=" "; 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上传表单文件
标签:
热心网友 时间:2022-04-30 06:37
//上传D盘下的test.jpg文件,文件必须存在,否则curl处理失败且没有任何提示
$data = array('name' => 'Foo', 'file' => '@d:/test.jpg');
注: PHP 5.5.0起,文件上传建议使用CURLFile代替@
$ch = curl_init('http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
更多内容请参考:http://www.zjmainstay.cn/php-curl#十模拟上传文件