PDA

View Full Version : Problems reading content of QFile



martinn
24th March 2010, 14:09
I need to send a sqlite file up to a website using post. So what I do is that I open the file and try to read the content of it into a QString that I with help of QNetworkRequest can send to the server. I can handle the request as the file is sent to the server but the file is just empty. The QString str here is always empty. Am I reading the content of the sqlite file wrong? Can you anything wrong with my code?

This is the code for reading the file:


QFile file("database.sql"); //I have tested that this excists
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
QString str;
in.setVersion(QDataStream::Qt_4_6);
in >> str; //str is always empty here

high_flyer
24th March 2010, 14:45
test file.open() for success.

martinn
24th March 2010, 16:14
Thanks but QFile.open() seems to want a parameter like QIODevice::ReadOnly. just open() isnt working for me. Was that what you meant? What I need to do is get the content of the file into a variable so that I can send it to server. Am I doing something wrong in my code?

JD2000
24th March 2010, 18:55
Please excuse my ignorance but I am not familiar with the intenals of a sqlite file. Is it binary?

In which case you are trying to feed binary data into a string.

JackHammer
24th March 2010, 19:51
@martinn

Check for success like this.
if( file.open(QIODevice::ReadOnly) )
{
//your code
}

Lykurg
24th March 2010, 20:29
Check if your path is right and you might better use QTextStream.

martinn
25th March 2010, 11:01
I updated my code with check that the file excist and is open. It is open but I still can't get the str to get all the content of the sqlite file.



QFile file("database.sql")
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
QString str;
in.setVersion(QDataStream::Qt_4_6);
in >> str; //str is always empty here
}


When testing with QTextStream instead it just set the str variable to "SQLite". What I need is the content of the file, how can I do that?

JD2000
25th March 2010, 12:11
Try opening the file in a text editor - Vi, Emacs, Notepad or what ever.

If you see gibberish then this is a binary file and can not be held in a string.

You will need to dump the sqlite file as text first and open that.

martinn
25th March 2010, 15:07
Yes it looks gibberish but how can I then send it to my server? I would have the same problem if I wanted to send an image or another binary file.

This is my code, should I do something different?


QByteArray data;
QFile file("database.sql");
if (!file.open(QIODevice::ReadWrite)) {
return;
}

QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_6);
in >> data ;

QString boundary;
QByteArray dataToSend; // byte array to be sent in POST

boundary="-----------------------------7d935033608e2";

QString body = "\r\n--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name=\"database\"; filename=\"database.sql\"\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += data;
body += "\r\n--" + boundary + "--\r\n";
dataToSend = body.toAscii();

QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this);
QNetworkRequest request(QUrl("http://www.mydomain.com/upload.aspx"));
request.setRawHeader("Content-Type","multipart/form-data; boundary=-----------------------------7d935033608e2");
request.setHeader(QNetworkRequest::ContentLengthHe ader,dataToSend.size());
connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(sendReportToServerReply(QNetworkReply*)));
QNetworkReply *reply = networkAccessManager->post(request,dataToSend); // perform POST request

Lykurg
25th March 2010, 15:15
Yes it looks gibberish
...then you should not use the sql file ending, since that indicates that it is a plain text file! Then forget about QTextStream and deal with QDataStream or better don't use any stream at all.

JD2000
25th March 2010, 18:09
To upload the binary file, just open it like you have in lines 2-3 and use 'file' in place of 'dataToSend'.

As Lykurg has pointed out, there is no need to use streams or convert the file to ascii.

If you are trying to upload text from a sqlite DB to a web page for people to read, you may need to run a select query to get the text out of the DB and then upload that.

martinn
26th March 2010, 12:35
Thanks! And I'm so sorry but I must be slow or something, I really don't understand where to place the file. :-)

Can you show me in the code I posted how I can send the file?

martinn
6th April 2010, 18:42
Anyone know how to solve this? Sorry but I really can't get it working..