Congratulations to our 2022 Qt Champions!

How to I transfer file with size more than 65KB?

  • Hi,

    I am trying to send a file, it has most size 65KB, but after receive it, it has only 65KB and the remainder of file is lost. Can someone help me?

    Send code:
    sendFile(const QString filename, const QString version)
    if (! connected)

        QFile fileSend(filename);
        if (
            QByteArray data(fileSend.readAll());
            Message msg;
                            << filename << version);
            QByteArray block;
            QDataStream out(&block, QIODevice::WriteOnly);
            out << (quint32)0;
            out << msg;
            out << (quint32)(block.size() - sizeof(quint32));
            quint64 xx = 0;
            while (xx < data.size())
               xx += tcpSocket->write(data);
           qWarning("data.size(): %u,  xx: %u \n", data.size(), xx);
            // tcpSocket->write(block);


    Receive code:

    receiveFile(Message msg)
    QList<QVariant> contents = msg.getContents();
    QString filename(;
    QFile fileReceived(filename);
    if (

        quint64 x = 0;
        QByteArray data(readAll());
        qWarning("data.size(): %u \n", data.size());
        while (x < data.size())
            x += fileReceived.write(data);


  • you might need a while loop in your receive part. At least that part is missing in your source snippet for receiving the information.

  • isnt 65kb the internal QIODevice buffersize? so koahnig might be right, you have to loop over it with in a readyRead slot with @read(bytesAvailable())@

  • The best way here is to either send total data size (so receiver will know how much it should receive) or send some boundary sequence (as multipart/form-data http content-type does)

  • Denis is right. I highly prefer the first solution. Saves you from escaping and related headaches, and allows for things like progress bars on the receiving end. Just send a quint64 that indicates the amount of data you will be sending before actually starting the send.

  • As addition to Andre. You of course need to add some timeouts on reading in case if network connection will be broken, etc. Or you will wait forever for this amount stored in quint64 :)

  • Well, if you start the discussion on how to do best. The question is also, do you really want to send just a big file? For a couple of 100 kB that is acceptable. For a couple of MB it might make sense to spend a little more thinking. Memory is not as expensive anymore as it was, but ultimately one is always reaching the boundaries.
    So cutting the file into chunks on both is certainly a good thing to think about.

Log in to reply