PDA

View Full Version : Upload request



dipeshtech
4th May 2011, 13:50
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()
{
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;

}




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);
?>


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.

squidge
4th May 2011, 14:29
You are correct in that you can not upload to the server as you have done. You need to upload in a format suitable for the server.

You may wish to research MIME encoding.

dipeshtech
4th May 2011, 17:55
Thanks squidge..!! But, still i am unable to figure out the right syntax by which i can include "data" in my request. Can i get some hint regarding that. I would be happy to know, if anyone can suggest me the wrong (in the context what i am trying to achieve) things which i have written in my code.

wysota
4th May 2011, 18:26
The data has to be in the body part of the request and not in the request header. If you are using x-www-form-url encoding then first learn what the encoding looks like before trying to use it.

dipeshtech
4th May 2011, 19:48
Thanks wysota!

But, I don't know how to include 'data' in body part. Actually, i don't how to include that in body part. I too had doubt on that point that i am doing wrong in including it in header. Help me please, i don't know how to do that in qt.

And, you are right there! I will research on understanding the form-url-encoding thing.

wysota
4th May 2011, 19:59
Thanks wysota!

But, I don't know how to include 'data' in body part. Actually, i don't how to include that in body part. I too had doubt on that point that i am doing wrong in including it in header. Help me please, i don't know how to do that in qt.

Read the docs for QNetworkAccessManager::post(). And use your head instead of blindly trying some random things.

dipeshtech
4th May 2011, 20:09
First of all i am not an expert and still very naive. I have read the docs of QNetworkAccessManager:: post() and i have used it in my code and also used MY HEAD [see above]. My question is how to identify the data of file with attribute 'data' [check the server side PHP script]. If you can answer that is fine for me.

wysota
4th May 2011, 20:17
If you can answer that is fine for me.
You have already been answered that:


You need to upload in a format suitable for the server.

And you even know how to do that:

I will research on understanding the form-url-encoding thing.

dipeshtech
4th May 2011, 20:36
I said "I will" . So, for now i don't know how to do that. That's why, urging you for help.

wysota
4th May 2011, 21:25
I said "I will".
So do that.


So, for now i don't know how to do that. That's why, urging you for help.
http://www.lmgtfy.com?q=x-www-form-urlencoded

squidge
5th May 2011, 00:25
If you want examples, you can use something like Etherpeek with a web page which allows you to upload data from your pc (eg. imageshack).

However, this will not tell you what YOUR server supports.

You can also use it once you have written your code to ensure it is sending the correct data. Its much better than trying to code "blind".

ChrisW67
5th May 2011, 02:50
http://www.w3.org/TR/html401/interact/forms.html

In particular, the section on "Form content types". For a large file (not text or more than a few tens of bytes) you typically want a Content-Type of "multipart/form-data" rather than "application/x-www-form-urlencoded" in your headers. Your request payload then needs to be encoded suitably.

dipeshtech
5th May 2011, 08:11
Thanks all. I did 2 mistakes one was not encoding the data to be sent and other was (silly) by not specifying the correct URL of the page. The code that works for me is below:




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;
}

file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray data(file.readAll());
QByteArray sdata;
QUrl params;
params.addQueryItem("data", data); //I wasn't encoding the data, one error was here
sdata.append(params.toString());


file.close();



QNetworkRequest request;

request.setUrl(QUrl("http://myserv.com/smsspam/smsspam.php")); // i committed a silly blunder here by not including th php page in URL ( which i came to know by checking the reply content)


request.setHeader(QNetworkRequest::ContentTypeHead er, "application/x-www-form-urlencoded");


QNetworkReply *reply = manager.post(request, sdata);
currentUploads.append(reply);
}

}

void uploadmanager::uploadFinished(QNetworkReply *reply)
{

if (reply->error())
{
QByteArray response = reply->readAll();
QFile file_spf("E:\\SMSAin\\dlog.txt");
file_spf.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream spf_in(&file_spf);
spf_in<<response;

QString message_er = QString("Error in Upload");
QMessageBox::information(this, "Err", message_er);
}
else
{
QString message_d = QString("Upload Complete");
QMessageBox::information(this, "Upload Complete", message_d);

}

currentUploads.removeAll(reply);
reply->deleteLater();

if (currentUploads.isEmpty())
return;

}