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

        QFile fileSend(filename);
        if (fileSend.open(QIODevice::ReadOnly))
        {
            QByteArray data(fileSend.readAll());
    
            Message msg;
            msg.setTypes(Message::UpdatePlugin);
            msg.setContents(QList<QVariant>()
                            << filename << version);
    
            QByteArray block;
            QDataStream out(&block, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_4_6);
    
            out << (quint32)0;
            out << msg;
    
            out.device()->seek(0);
            out << (quint32)(block.size() - sizeof(quint32));
    
            tcpSocket->write(block);
    
            quint64 xx = 0;
            while (xx < data.size())
               xx += tcpSocket->write(data);
    
           qWarning("data.size(): %u,  xx: %u \n", data.size(), xx);
    
            // tcpSocket->write(block);
            tcpSocket->flush();
        }
    

    }
    @

    Receive code:
    @

    receiveFile(Message msg)
    {
    QList<QVariant> contents = msg.getContents();
    QString filename(contents.at(0).toString());
    QFile fileReceived(filename);
    if (fileReceived.open(QIODevice::Append))
    {

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

    }
    @


  • Moderators

    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 :)


  • Moderators

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.