添加变量
QNetworkAccessManager* manage;
QNetworkRequest req;
QByteArray data ;
构造函数中
manage = new QNetworkAccessManager(界面);
实现
QString url=QString("http://服务器地址/file.php?filename=%1&w=%2&e=%3").arg(str).arg(str_w).arg(“xxx”);
req.setUrl(QUrl(url));
req.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
QFile file("backandpict/"+str);
if(!file.open(QIODevice::ReadOnly))
{
qDebug()<<"file open failed";
}
data = file.readAll();
file.close();
manage->post(req,"fileData="+data.toBase64()+"");
post结束会有一个结束函数
connect(manage,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
最后实现向服务器传送参数和文件
文件只能是小文件,传送大文件的时候会很慢
服务器接收 接收文件写法不同
方式一:
<?php
$recContent=$_POST['fileData'];
$name = $_GET["filename"];
$sendweixin =$_GET["w"];
$sendemail=$_GET["e"];
$data=base64_decode(str_replace(" ","+",($recContent)));
file_put_contents("image/".date("y-m-d",time()).$name, $data);
$pict_name = date("y-m-d",time()).$name;
?>
方式二:
<?php
$c = $GLOBALS['HTTP_RAW_POST_DATA'];
$n = $_GET["filename"];
$fp = fopen($n,'w+');
fwrite($fp, $c, strlen($c));
fclose($fp);
file_put_contents("file.xml", date('Y-m-d H:i:s ').$n."\r\n", FILE_APPEND);
?>