Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to I transfer file with size more than 65KB?
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 7.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tchoninho
    wrote on last edited by
    #1

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

    }
    @

    Computer Scientist
    Belo Horizonte - Brasil
    C++ Development
    Qt Development

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

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

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Jupiter
        wrote on last edited by
        #3

        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())@

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                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.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved