我将几个二进制数据与QNetworkAccessManager一起发送到php Url。收到数据后,php页面进一步将一些数据发送回处理后强制下载。我可以看到成功发布到服务器,但之后我在QNetworkReply中没有得到正确的响应,我没有看到它发出任何信号。
//mUploadData is QByteArray
mUploadData.append(QString("--" + mBoundary + "\r\n").toAscii());
mUploadData += "Content-Disposition: form-data; name=\"UserData\";
filename=\"UserData.trprof\"\r\n";
mUploadData += "Content-Type: application/octet-stream\r\n\r\n";
mUploadData += encryptedPostData;
mUploadData += "\r\n";
mUploadData += QString("--" + mBoundary + "\r\n").toAscii();
mUploadData += "Content-Disposition: form-data; name=\"upload\"\r\n\r\n";
mUploadData += "Uploader\r\n";
mUploadData += QString("--" + mBoundary + "--\r\n").toAscii();
QUrl serviceUrl = QUrl( "http://localhost/user_data/listen_post.php" );
QNetworkRequest request(serviceUrl);
request.setRawHeader("Content-Type","multipart/form-data; boundary=---------------------------193971182219750");
// post data to the server.
mNetworkReply = mNetworkAccessManager.post( request , mUploadData );
mDownloadedBytes.clear();
connect( mNetworkReply , SIGNAL( finished() ) , this , SLOT( httpFinished() ) );
connect( mNetworkReply , SIGNAL( readyRead() ) , this , SLOT( httpReadyRead() ) );
connect( mNetworkReply , SIGNAL( error( QNetworkReply::NetworkError ) ) , this , SLOT( printError( QNetworkReply::NetworkError ) ) );
这是php代码:
<?php
$file = 'test.txt';
$post_info = file_get_contents($_FILES['UserData']['tmp_name']);
file_put_contents( $file , $decrypted );
$filename = 'test2.txt';
if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
file_put_contents( $file , filesize($filename) , FILE_APPEND );
readfile($filename);
exit;
}
?>
请注意,我已经使用QNetworkAccessManager get方法测试了php下载代码,并且只是在浏览器中打开页面,在这两种情况下我都得到了正确的下载,但只有在Qt发布的情况下我才得到任何东西。没有信号发出,所以我甚至无法判断是否发生了错误,我只是一无所知,发生了什么。
真的很感激任何帮助。
此致 Avanindra Singh
答案 0 :(得分:0)
我不确定你为什么没有收到错误。有一个多部分数据的包装器,如果问题在于格式化,这可能会有所帮助。 https://qt-project.org/doc/qt-4.8/qhttpmultipart.html
您是否检查过PHP日志或使用Wireshark / tcpdump来查看请求是否看起来格式正确?
答案 1 :(得分:0)
感谢nickrak,快速回复。我解决了问题。由于对象的范围,我似乎面临着无信号的问题,我将数据发布到服务器。一旦我将其设为非本地,我就开始正确接收信号。