Howto neglect these bytes while downloading file from http server
-
wrote on 1 Feb 2022, 11:00 last edited by
The four bytes look like the number of bytes in the remainder of the message.
-
wrote on 1 Feb 2022, 11:13 last edited by
ChrisW67: You are right. That's the data length. The problem was in the below lines:
QDataStream out(&file); out << bytes;
When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).
So i figured out to use QDataStream::writeRawData
QDataStream out(&file); size_t size = bytes.size(); out.writeRawData(bytes, size); file.close();
-
Why do you need a QDataStream then at all?
-
wrote on 1 Feb 2022, 12:38 last edited by
I don't actually need it if i just use it for downloading a file, In fact, it is easier to just use QFile::write(..) in the above case. I though it would be easier to prefix/suffix files content like below:
out << prefix;
out << file;
out << suffix -
If you use QDataStream on the sender side you must also use it on the receiver side. Everything else will break sooner or later.
-
wrote on 1 Feb 2022, 16:15 last edited by
@Christian-Ehrlicher : I didn't notice that, can you please elaborate it a little.
-
Don't know what else I should say - you use QDataStream to create your stream so you have to use QDataStream to deserialize the data later on. QDataStream adds data to the stream (as you already noticed) and may alter your data in any way: "A data stream is a binary stream of encoded information which is 100% independent of the host computer's operating system, CPU or byte order. For example, a data stream that is written by a PC under Windows can be read by a Sun SPARC running Solaris."
So reading it as raw data may or may not work. The format of QDataStream's binary representation is not documented so you it can change. -
wrote on 1 Feb 2022, 16:35 last edited by
oky. thank you :)
-
ChrisW67: You are right. That's the data length. The problem was in the below lines:
QDataStream out(&file); out << bytes;
When bytes are written to QDataStream in this fashion, Its write the content length followed by the actual data. (I thought it will just write the content).
So i figured out to use QDataStream::writeRawData
QDataStream out(&file); size_t size = bytes.size(); out.writeRawData(bytes, size); file.close();
12/12