Results 1 to 5 of 5

Thread: Upload files bigger than available memory

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Post Upload files bigger than available memory

    Can someone please explain to me how to upload files which are bigger than available memory?
    I looked at every example and they all load complete files with readAll() and then send them.
    This is not an option for me on S60 platform coz I hit Out-of-memory error.
    I tried to do it with QNetworkAccessManager:: post() method and I failed.
    Now, I'm trying to use QTcpSocket, but I don't know how to send and format the header before sending content??
    I attached a part of my code:
    Qt Code:
    1. tcpSocket = new QTcpSocket();
    2.  
    3. QString param = "upload.txt";
    4.  
    5. QNetworkAccessManager *http_upload = new QNetworkAccessManager(this);
    6. QNetworkRequest requpl;
    7.  
    8. QString toDropboxDir = metadata.value("path");
    9. if (!toDropboxDir.isEmpty())
    10. {
    11. toDropboxDir = toDropboxDir.right(toDropboxDir.size()-1);
    12. }
    13.  
    14. std::string stuff_std = OAuthWebRequestUpload("http://api-content.dropbox.com/0/files/dropbox/"+toDropboxDir.toStdString(),"POST",consumerKey,consumerSecret,token,secret,"",param.toStdString());
    15. QString stuff = QString::fromStdString(stuff_std);
    16. QByteArray stuff_BA;
    17. stuff_BA = stuff.toAscii();
    18.  
    19. qsrand(QDateTime::currentDateTime().toTime_t());
    20. QString boundary_random = QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
    21. QString crlf("\r\n");
    22. QString boundaryStr("---------------------------"+boundary_random);
    23. QString boundary="--"+boundaryStr+crlf;
    24.  
    25. tcpSocket->connectToHost("api-content.dropbox.com",80);
    26. connect(tcpSocket, SIGNAL(connected()), this, SLOT(filler()));
    27. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erase_multipartform()));
    28.  
    29. //setting header to the request (with neccessary OAuth authorization)
    30. requpl.setUrl(QUrl(QString("http://api-content.dropbox.com/0/files/dropbox/"+metadata.value("path"))+"?file="+param));
    31. requpl.setRawHeader("Authorization", stuff_BA);
    32. requpl.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary="+boundaryStr);
    33. QFile *file_upload = new QFile(param_path);
    34. file_upload->open(QIODevice::ReadOnly);
    35.  
    36. //sending content
    37. QByteArray payload;
    38. QDataStream stream(&payload, QIODevice::WriteOnly);
    39. stream.setVersion(QDataStream::Qt_4_7);
    40. stream << "Authorization "+stuff;
    41. qDebug() << "Authorization "+stuff;
    42. stream << "--"+boundaryStr << "\r\n";
    43. //qDebug() << "--"+boundaryStr << "\r\n";
    44. stream << "Content-Disposition: form-data; name=\"file\"; filename=\""+param+"\"" << "\r\n";
    45. //qDebug() << "Content-Disposition: form-data; name=\"file\"; filename=\""+param+"\"" << "\r\n";
    46. stream << "Content-Type: text/plain" << "\r\n" << "\r\n";
    47. //qDebug() << "Content-Type: "+param_mime << "\r\n" << "\r\n";
    48.  
    49. int iBlock = 0;
    50. while(!file_upload->atEnd())
    51. {
    52. qDebug()<<"Reading Line: "<<++iBlock;
    53. //stream << (quint16)0;
    54. //stream << file_upload->readLine().data();
    55. stream << file_upload->readAll().data();
    56. tcpSocket->write(payload);
    57. tcpSocket->flush();
    58. //qDebug() << QString(file_upload->readAll().constData());
    59. payload.clear();
    60.  
    61. }
    62. file_upload->close();
    63. stream << "\r\n" << "--"+boundaryStr+"--" << "\r\n";
    64. //qDebug() << "\r\n" << "--"+boundaryStr+"--" << "\r\n";
    65. reply_upload = http_upload->post(requpl,payload);
    66. connect(reply_upload, SIGNAL(finished()), this, SLOT(readData_upload()));
    To copy to clipboard, switch view to plain text mode 

    Please help me, I'm stuck on this for days now...
    Last edited by knobtviker; 19th December 2010 at 12:39.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Upload files bigger than available memory

    Did you try the QNetworkAccessManager::post() that takes a QIODevice as a parameter?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Upload files bigger than available memory

    Hi, thanks for answering.
    Yes I tried it with that method but then I can't load the boundarys like that and that implies (if I understand the docs properly) that the file still loads up entirely which is fine for small files but not for the large ones.

    I rewrote my code, went low level on this one and learned a lot. But it's still not working, somehow something is either wrong with my generated POST form or the TCP socket is not connected properly.
    Take a look and tell me what I'm missing out:
    Qt Code:
    1. //TESTING PARAMETERS START
    2. QString param = "upload.txt";
    3. //QString param = "Zastitaokolisa.rar";
    4.  
    5. QString param_mime = "text/plain";
    6. //QString param_mime = "application/x-rar-compressed";
    7.  
    8. QString param_path = "E://Dropian//"+param;
    9. //QString param_path = "E://"+param;
    10. //TESTING PARAMETERS END
    11.  
    12. //SETUP TCPSOCKET AND DATA
    13. QString toDropboxDir = metadata.value("path");
    14. if (!toDropboxDir.isEmpty())
    15. {
    16. toDropboxDir = toDropboxDir.right(toDropboxDir.size()-1);
    17. }
    18.  
    19. std::string stuff_std = OAuthWebRequestUpload("http://api- content.dropbox.com/0/files/dropbox/"+toDropboxDir.toStdString(),"POST",consumerKey,consumerSecret,token,secret,"",param.toStdString());
    20. QString stuff = QString::fromStdString(stuff_std);
    21. QByteArray stuff_BA;
    22. stuff_BA = stuff.toAscii();
    23.  
    24. qsrand(QDateTime::currentDateTime().toTime_t());
    25. QString boundary_random = QVariant(qrand()).toString()+QVariant(qrand()).toString()+QVariant(qrand()).toString();
    26. QString crlf("\r\n");
    27. QString boundaryStr("---------------------------"+boundary_random);
    28. QString boundary="--"+boundaryStr+crlf;
    29.  
    30. tcpSocket = new QTcpSocket();
    31.  
    32. tcpSocket->connectToHost("api-content.dropbox.com",80);
    33. connect(tcpSocket, SIGNAL(connected()), this, SLOT(filler()));
    34. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erase_multipartform()));
    35.  
    36.  
    37. QFile *file_upload = new QFile(param_path);
    38. file_upload->open(QIODevice::ReadOnly);
    39.  
    40. QString header;
    41. header += "POST /0/files/dropbox/"+metadata.value("path")+" HTTP/1.1\r\n";
    42. header += "Host: api-content.dropbox.com\r\n";
    43. header += "Content-Type: multipart/form-data; boundary="+boundaryStr+"\r\n";
    44. header += "Authorization: "+stuff+"\r\n\r\n";
    45.  
    46. QString content;
    47. content += "--"+boundaryStr+"\r\n";
    48. content += "Content-Disposition: form-data; name=\"file\"; filename=\""+param+"\"\r\n";
    49. content += "Content-Type: "+param_mime+"\r\n\r\n";
    50.  
    51. QString end;
    52. end += "\r\n--"+boundaryStr+"--\r\n";
    53.  
    54. QByteArray payload;
    55. QDataStream stream(&payload, QIODevice::WriteOnly);
    56. stream.setVersion(QDataStream::Qt_4_7);
    57.  
    58. //HEADER STREAM START
    59. stream << header;
    60. qDebug() << header;
    61. //tcpSocket->waitForConnected(30000);
    62. tcpSocket->write(payload);
    63. tcpSocket->flush();
    64. payload.clear();
    65. header.clear();
    66. //HEADER STREAM END
    67.  
    68. //CONTENT STREAM START
    69. stream << content;
    70. qDebug() << content;
    71. //tcpSocket->waitForConnected(30000);
    72. tcpSocket->write(payload);
    73. tcpSocket->flush();
    74. payload.clear();
    75. content.clear();
    76.  
    77. //UPLOADING FILE START
    78. int iBlock = 0;
    79. while(!file_upload->atEnd())
    80. {
    81. qDebug()<<"Reading Line: "<<++iBlock;
    82. //stream << (quint16)0;
    83. stream << file_upload->readLine().data();
    84. //stream << file_upload->readAll().data();
    85. //tcpSocket->waitForConnected(30000);
    86. tcpSocket->write(payload);
    87. tcpSocket->flush();
    88. //qDebug() << QString(file_upload->readLine().constData());
    89. //stream.device()->seek(0);
    90. //stream << (quint16)(payload.size()-sizeof(quint16));
    91. payload.clear();
    92.  
    93. }
    94. file_upload->close();
    95. //UPLOADING FILE END
    96.  
    97. stream << end;
    98. qDebug() << end;
    99. //tcpSocket->waitForConnected(30000);
    100. tcpSocket->write(payload);
    101. tcpSocket->flush();
    102. payload.clear();
    103. end.clear();
    104. //CONTENT STREAM END
    105.  
    106. //tcpSocket->waitForConnected(30000);
    107. tcpSocket->disconnect();
    To copy to clipboard, switch view to plain text mode 

    And here is the POST form it generates:
    Qt Code:
    1. POST /0/files/dropbox/ HTTP/1.1
    2. Host: api-content.dropbox.com
    3. Content-Type: multipart/form-data; boundary=---------------------------63101371611732759261053641528
    4. Authorization: OAuth file=upload.txt,oauth_consumer_key=1234567890abcdef,oauth_nonce=Kqm88U9yhT7XfeL0,oauth_signature=WTXVsyETru0GR%2BiK5ZxW9MNuGG8%3D,oauth_signature_method=HMAC-SHA1,oauth_timestamp=1292846464,oauth_token=1234567890abcdef,oauth_version=1.0
    5.  
    6. -----------------------------63101371611732759261053641528
    7. Content-Disposition: form-data; name="file"; filename="upload.txt"
    8. Content-Type: text/plain
    9.  
    10. Hello world!
    11.  
    12. -----------------------------63101371611732759261053641528--
    To copy to clipboard, switch view to plain text mode 

    I admit, I'm a n00b at QTcpSocket so don't laugh too hard at this.
    Thanks for your help in advance.

  4. #4
    Join Date
    Jun 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Upload files bigger than available memory

    Hi all,

    did you ever find a solution to this problem?

    Rgds Henrik

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Upload files bigger than available memory

    Did you read Wysota's post?

Similar Threads

  1. Selected row bigger than others in QTableView
    By JuanMO in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2010, 16:48
  2. Do resources in QRC files consume memory?
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 7th December 2009, 08:07
  3. upload movie files to youtube
    By jay in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2009, 12:34
  4. upload files using QNetworkAccessManager
    By Raajesh in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2008, 19:43
  5. Replies: 2
    Last Post: 23rd November 2007, 17:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.