我使用 QNetworkAccessManager 将一些二进制数据发送到 php Url。php页面接收到数据后,进一步处理后将一些数据作为强制下载发回。我可以看到成功发布到 server ,但之后我在 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 发布的情况下,我什么也得不到。没有发出信号,所以我什至无法判断是否发生了错误,我简直一无所知,这是怎么回事。
任何帮助将不胜感激。
问候阿文德拉·辛格