I am trying to upload a file on the server. I have written this code, but i am receiving error in reply from server (on call of uploadfinished() [in code])
uploadmanager::uploadmanager()
{
connect(&manager, SIGNAL(finished(QNetworkReply*)),
SLOT(uploadFinished(QNetworkReply*)));
}
void uploadmanager::doUpload()
{
args<<"E:\\SMS\\spam_freq.csv";
{
return;
}
QNetworkRequest request;
request.
setUrl(QUrl("me.xyz.com/smsspam"));
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
request.setRawHeader("Accept","text/plain");
file.close();
request.setRawHeader("data",fileBinaryData);
QNetworkReply *reply = manager.post(request, fileBinaryData);
currentUploads.append(reply);
}
}
void uploadmanager::uploadFinished(QNetworkReply *reply)
{
if (reply->error())
{
QMessageBox::information(this,
"Err", message_er
);
//I am getting this msg }
else
{
QMessageBox::information(this,
"Upload Complete", message_d
);
}
currentUploads.removeAll(reply);
reply->deleteLater();
if (currentUploads.isEmpty())
return;
}
uploadmanager::uploadmanager()
{
connect(&manager, SIGNAL(finished(QNetworkReply*)),
SLOT(uploadFinished(QNetworkReply*)));
}
void uploadmanager::doUpload()
{
QStringList args;
QString message_d = QString("Unable to open file");
args<<"E:\\SMS\\spam_freq.csv";
foreach (QString arg, args) {
QFile file(arg);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(this, "Error", message_d);
return;
}
QNetworkRequest request;
request.setUrl(QUrl("me.xyz.com/smsspam"));
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
request.setRawHeader("Accept","text/plain");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray fileBinaryData(file.readAll());
file.close();
request.setRawHeader("data",fileBinaryData);
QNetworkReply *reply = manager.post(request, fileBinaryData);
currentUploads.append(reply);
}
}
void uploadmanager::uploadFinished(QNetworkReply *reply)
{
if (reply->error())
{
QString message_er = QString("Error in Upload");
QMessageBox::information(this, "Err", message_er); //I am getting this msg
}
else
{
QString message_d = QString("Upload Complete");
QMessageBox::information(this, "Upload Complete", message_d);
}
currentUploads.removeAll(reply);
reply->deleteLater();
if (currentUploads.isEmpty())
return;
}
To copy to clipboard, switch view to plain text mode
The PHP script at the server is below:
<?php
// Read the raw http data from the post
$data = $_POST['data'];
// Ungzip the wav file
// $chunk = gzuncompress($data);
if (!$data) {
echo -1;
die();
}
$uid = uniqid();
$filename = "$uid.csv";
$filepathname = "uploads/$filename";
$handle = fopen($filepathname, 'wb');
fputs($handle, $data, strlen($data));
fclose($handle);
?>
<?php
// Read the raw http data from the post
$data = $_POST['data'];
// Ungzip the wav file
// $chunk = gzuncompress($data);
if (!$data) {
echo -1;
die();
}
$uid = uniqid();
$filename = "$uid.csv";
$filepathname = "uploads/$filename";
$handle = fopen($filepathname, 'wb');
fputs($handle, $data, strlen($data));
fclose($handle);
?>
To copy to clipboard, switch view to plain text mode
Kindly, help me in constructing packet header and request in correct format as per the script. I think, i am not doing that in right way.
Any help would be appreciated.
Bookmarks