PDA

View Full Version : Prob with store and retrieve binary data to/from remote sqlserver's image field



Askar
25th September 2009, 06:34
Dear Friends ,
I need to store and retrieve binary data from remote sql server's image field. now i can store binary value into remote server by simply reading the source file sontent into byte array and store that byte array content into a Qstring while usnig insert statement.


/************code***********/

wBData is a byte array in which i read source file content
QString s1(wBData);
net.get("http://192.168.0.220/clancor?sql=insert+into+blob+(sno,photo)+values+(4 ,'"+ s1 +"')&root=blobinsert"

/*************************/

while retrieving sql server gives image field as a Binary base Encoded format i tried to docode.but decoded value does not creates original file i saved into the database


/********code**************/

QByteArray wBData1;
net.get("http://192.168.0.220/clancor?sql=select+photo+from+blob+where+sno+=+4+F OR+XML+RAW,+BINARY+BASE64&root=blobinsert&contentt ype=text/xml")

wBData1.append(xml.attributes().value("photo").toS tring());

QByteArray by(QByteArray::fromBase64(wBData1));
fil.write(by); // write into a file

/*************************/
i wondered if any body notify where im doing wrong?

Thanks

Askar

wysota
26th September 2009, 17:21
What is "net" and how does it work?

Askar
29th September 2009, 06:58
Dear Wysota,

net is QNetworkAccessManager object;


/******************************/

QNetworkAccessManager net = new QNetworkAccessManager();

/******************************/

thanks,

wysota
29th September 2009, 08:40
And where do you retrieve data from it? Remember it works in an asynchronous fashion.

Askar
29th September 2009, 11:02
Dear Wysota,
Im retrieving binary data from remote sql server (priviously stored binary data for a image file). and tried to reconvert it into a image file again.
and what do u mean by works in an asynchronous fashion?

wysota
29th September 2009, 11:22
Where in your code...

Askar
29th September 2009, 16:19
/********code**************/
QNetworkAccessManager net = new QNetworkAccessManager();
QByteArray wBData1;
net.get("http://192.168.0.220/clancor?sql=select+photo+from+blob+where+sno+=+4+F OR+XML+RAW,+BINARY+BASE64&root=blobinsert&contentt ype=text/xml")

wBData1.append(xml.attributes().value("photo").toS tring());

QByteArray by(QByteArray::fromBase64(wBData1));
fil.write(by); // write into a file

/*************************/

wysota
29th September 2009, 18:17
I still fail to see where you retrieve data from the "net" object. If you think QNetworkAccessManager would somehow magically guess that you wanted it to place the contents of the retrieved file in "wBData1" variable then you're wrong. And again, QNetworkAccessManager works in an asynchronous fashion. See it's docs for examples of usage.

Askar
30th September 2009, 06:47
Dear Wysota,
By using net object im passing sql query to sql server, to retrive records from database tables,

/***********************/
QEventLoop loop;
QNetworkReply *reply;
reply=net.get("http://192.168.0.220/clancor?sql=select+photo+from+blob+where+sno+=+4+F OR+XML+RAW,+BINARY+BASE64&root=blobinsert&contentt ype=text/xml")
connect(reply, SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec(); //entering event loop until reply object's finished() signal gets activated

QXmlStreamReader xml(reply->readAll()); //storing retrieved xml file content xmlstreamreader object


/**********************/

By using xml parsing functions i am reading the actual record (binary base64) values retrieved from the table by using the following line.
/******************************/
wBData1.append(xml.attributes().value("photo").toString());
/*****************************/
this line reads data from xml object into Byte array variable WBData1

------------------by this way only im retrieving data from net object------------------

wysota
30th September 2009, 08:27
Now we're getting somewhere... So what happens after you put the data into the byte array? Where are your sql statements here and what is wrong exactly?

I'm now pretty annoyed with having to drag every information out of you almost by force. Either you provide information and code for others to see what is going on in your code or I'm done answering to this thread.

Askar
30th September 2009, 12:30
Sorry Mr.Wysota
Its my mistake, i should post my thread with clear codes and explanations. sorry once again.

the retrieved value from xml file is in binary base64 encoded, so i created another one byte array (byte) to store the base64 decoded binary data, for that i used the following syntax...


QByteArray byte(QByteArray::fromBase64(wBData1));

then im storing the contents of the variable byto into a already opened file, code given below



fil.write(byte);
fil.close();


fil is file object already opened,

after doing all these things i could not get the original file (for eg image) what im stored before into the server.

wysota
30th September 2009, 12:34
How does your xml parser look like? If the code you pasted is complete, it means that you don't have a parser and you "forgot" to read QXmlStreamReader documentation which you should now do.

Askar
30th September 2009, 13:02
sorry
i didnt pasted here the code for xml parsing .
now i gave the complete code for retrieving data from remote sql server & parsing the retrieved xml file.



QEventLoop loop;
QNetworkReply *reply;
QFile fil("testsnd");
if(!fil.open(QIODevice::WriteOnly))
{
qDebug() << "cannot open file for writing"<<qPrintable(fil.errorString()) <<endl;
return 0;
}
reply=net.get("http://192.168.0.220/clancor?sql=select+photo+from+blob+where+sno+=+4+F OR+XML+RAW,+BINARY+BASE64&root=blobinsert&contentt ype=text/xml")
connect(reply, SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec(); //entering event loop until reply object's finished() signal gets activated

QXmlStreamReader xml(reply->readAll()); //storing retrieved xml file content xmlstreamreader object
while (!xml.atEnd())
{
xml.readNext();
if (xml.tokenType() == QXmlStreamReader::StartElement)

if (xml.name() == "row")
{
wBData1.append(xml.attributes().value("photo").toString());
}
}
QByteArray byte(QByteArray::fromBase64(wBData1));
fil.write(byte);
fil.close();

wysota
30th September 2009, 14:27
now i gave the complete code for retrieving data from remote sql server & parsing the retrieved xml file.
You could have done that 12 posts ago...

Are you sure "photo" is an attribute and not an element?

Askar
30th September 2009, 14:53
ya sure............

wysota
30th September 2009, 14:58
What does this return?

qDebug("%d", wBData1.size());
qDebug("%d", byte.size());

Askar
30th September 2009, 15:15
wBData1.size() returns-----------12
byte.size() returns------------------7

wysota
30th September 2009, 15:53
Somehow I doubt you can fit an image with all its headers in 7 bytes...

Askar
1st October 2009, 16:51
dear Wysota,

while inserting binary value into the remote server database table with the following code


QString str;
QFile fileIn ("Sunset.jpg");
QByteArray wBData;
net = new QNetworkAccessManager();
if (fileIn.open (QIODevice::ReadOnly))
{
wBData.append(fileIn.readAll());

}
qDebug()<<wBData.size(); //returns 59129
str.append(wBData)
net.get("http://192.168.0.220/clancor?sql=insert+into+blob+(sno,photo)+values+(4 ,'"+ str +"')&root=blobinsert"



qDebug returns value 59129

but while retrieving same binary data value from remote server qdebug returns

wBData1.size() returns-----------12
byte.size() returns------------------7

which is entered in the previous thread

wysota
1st October 2009, 17:26
What's the whole xml you get from the server? Also how do you know the image is stored correctly? You are not encoding the data here anywhere...

Askar
5th October 2009, 06:51
Dear Wysota;
yes im not encoding the data while storing, but im storing the contents of byte array contents into a QString object while inserting the binary value into the remote server ( the code i gave in previous thread).

the whole xml contents which im retrieving are given below,

<?xml version="1.0" encoding="utf-8" ?><blobinsert><row photo="w7/DmMO/w6A="/></blobinsert>


from the xml file i retrieved from the server the base64 encoded binary contents are
w7/DmMO/w6A=

Please let me know, where im getting wrong ? and what is the correct way to do this process

Thanks,

Askar
6th October 2009, 15:06
Dear Wysota;
yes im not encoding the data while storing, but im storing the contents of byte array contents into a QString object while inserting the binary value into the remote server ( the code i gave in previous thread).

the whole xml contents which im retrieving are given below,



<?xml version="1.0" encoding="utf-8" ?><blobinsert><row photo="w7/DmMO/w6A="/></blobinsert>


from the xml file i retrieved from the server the base64 encoded binary contents are
w7/DmMO/w6A=

Please let me know, where im getting wrong ? and what is the correct way to do this process

Thanks,