Results 1 to 16 of 16

Thread: Issue with download from url

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Issue with download from url

    Qt Code:
    1. nreply->readAll()
    To copy to clipboard, switch view to plain text mode 

    This line is throwing an error as below only for windows machine. The same code works fine for mac.
    Invalid parameter passed to C runtime function.
    Invalid parameter passed to C runtime function.
    terminate called after throwing an instance of 'std::bad_alloc'
    what(): std::bad_alloc



    The download code is as below
    Qt Code:
    1. zoushoFtpTransfer :: zoushoFtpTransfer()
    2. {
    3. m_manager = new QNetworkAccessManager();
    4. m_request = new QNetworkRequest;
    5. m_reply = NULL;
    6.  
    7. m_isDownloadSccess = false;
    8. connect(m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
    9. this, SLOT(handleSslErrors(QNetworkReply*,QList<QSslError>)));
    10. connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyfinished(QNetworkReply*)));
    11. m_htmlDownloadCount = 0 ;
    12. }
    13. void zoushoFtpTransfer :: downloadFromServer()
    14. {
    15. QNetworkRequest request;
    16. QUrl url;
    17. url.setUrl(m_url);
    18. url.setUserName(m_username);
    19. url.setPassword(m_password);
    20. request.setUrl(url);
    21. zoushoConstructFilePath();
    22. zoushoConstructDownloadDir();
    23. zoushoConstructDownloadPath();
    24. m_reply = m_manager->get(request);
    25. }
    26.  
    27. void zoushoFtpTransfer :: replyfinished(QNetworkReply *nreply)
    28. {
    29.  
    30. if(nreply->error() )
    31. {
    32. qDebug() << "download failed...." <<nreply->errorString();
    33. m_isDownloadFailure = true;
    34. }
    35. else
    36. {
    37. //m_url = "http://www.ssparkl.com/download.php?filename=fullbook/LifeSciences_Grade10_1947.zip";
    38.  
    39. if(m_url.endsWith(".zip"))
    40. {
    41. QFile file(m_localPath);
    42. file.open(QIODevice::WriteOnly);
    43. file.write(nreply->readAll());
    44. file.close();
    45. m_isDownloadSccess = true;
    46. return;
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

    The download file size is 318459022.
    I checked the received data using the below code and it final received size is 318459022.
    Qt Code:
    1. connect(m_WebCtrl.get(request), SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadprogress(qint64,qint64)));
    To copy to clipboard, switch view to plain text mode 
    Book with size of 20MB are getting downloaded without issues.

    Kindly help me to know, where I am going wrong
    Last edited by ejoshva; 13th April 2015 at 06:28.

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

    Default Re: Issue with download from url

    You are trying to download a 3GB file and then make a copy of it using readAll(). How much RAM do you have in your machine? Is it at least 8GB? Does your Windows box allow you to allocate that much in a single process?
    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 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Issue with download from url

    My guess is you're running out of memory trying to read the entire response at once with QNetworkReply::readAll. Try using the QNetworkReply::readyRead() signal to read data from the request and write data to the file as it becomes available rather than waiting until the entire file has been received and trying to read it all at once.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with download from url

    Have you tried writing the data when it arrives instead of waiting for all of it?

    Cheers,
    _

  5. #5
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    Tried with readyRead() but got the error as QIODevice not open

    Quote Originally Posted by wysota View Post
    You are trying to download a 3GB file and then make a copy of it using readAll(). How much RAM do you have in your machine? Is it at least 8GB? Does your Windows box allow you to allocate that much in a single process?
    I had a doubt on this perspective. My machine RAM is 4GB.

    In that case, I would have write into file as and when it is received


    Added after 5 minutes:


    Quote Originally Posted by anda_skoa View Post
    Have you tried writing the data when it arrives instead of waiting for all of it?

    Cheers,
    _
    How do I do that?
    Last edited by ejoshva; 13th April 2015 at 08:49.

  6. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Issue with download from url

    Quote Originally Posted by ejoshva View Post
    How do I do that?
    With the readyRead() signal...

    Show the code when you tried to use readyRead() please.

  7. #7
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    Qt Code:
    1. connect(m_reply,SIGNAL(readyRead()),this,SLOT(readingReadyBytes()));
    2.  
    3. m_reply = m_manager->get(m_request);
    4.  
    5. void zoushoFtpTransfer::readingReadyBytes()
    6. {
    7. QFile file(m_localPath);
    8. file.write(m_reply->read(m_reply->bytesAvailable()));
    9. file.close();
    10. qDebug()<<"size : "<<file.size();
    11. m_isDownloadSccess = true;
    12. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Issue with download from url

    Move your connect statement *after* the get request. Also, where do you open file for writing?

    Edit: If you are going to write the file in the way you're attempting, you need to make sure you open the file to append data to the end of the file, or else each write will overlay prior data you've written.

  9. The following user says thank you to jefftee for this useful post:

    ejoshva (13th April 2015)

  10. #9
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    Thanks pin pointing the file:pen().
    That was the issue, now it's getting written to file.
    But only the last received alone is written to the file.

    Qt Code:
    1. m_reply = m_manager->get(m_request);
    2.  
    3. connect(m_reply,SIGNAL(readyRead()),this,SLOT(readingReadyBytes()));
    4.  
    5.  
    6. void zoushoFtpTransfer::readingReadyBytes()
    7. {
    8. QFile file(m_localPath);
    9. file.open(QIODevice::writeOnly);
    10. file.write(m_reply->read(m_reply->bytesAvailable()));
    11. file.close();
    12. qDebug()<<"size : "<<file.size();
    13. m_isDownloadSccess = true;
    14. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Issue with download from url

    See the rest of the previous comment.
    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.


  12. The following user says thank you to wysota for this useful post:

    ejoshva (13th April 2015)

  13. #11
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    by making use of QNetworkReply::readyRead() the issue is resolved.

    Thank for you timely replies and suggestion for the fix

  14. #12
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    Now I am able to download and write all the content using the readyRead()
    Qt Code:
    1. m_reply = m_manager->get(m_request);
    2.  
    3. connect(m_reply,SIGNAL(readyRead()),this,SLOT(readingReadyBytes()));
    4.  
    5.  
    6. void zoushoFtpTransfer::readingReadyBytes()
    7. {
    8. QFile file(m_localPath);
    9. file.open(QIODevice::writeOnly);
    10. file.write(m_reply->read(m_reply->bytesAvailable()));
    11. file.close();
    12. qDebug()<<"size : "<<file.size();
    13. m_isDownloadSccess = true;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Again there is one more issue, after writing onto file, I am storing all the data for that file in QByteArray.
    Its storing in QBytearray till 134459022 of data, after which bad_alloc error is thrown.

    Let me know what data type can be used to store all the 318459022 size of data

  15. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with download from url

    Quote Originally Posted by ejoshva View Post
    Let me know what data type can be used to store all the 318459022 size of data
    It is not a matter of data type. QByteArray can easily handle that, your system can't.

    Cheers,
    _

  16. #14
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Issue with download from url

    This last snippet of code you posted is exactly the same as the previous one, and suffers from exactly the same problems that were pointed to you. Since you claim to have now solved these problems, I suppose that this code is irrelevant to your new question.

    If you have understood that you could not keep the whole file in memory before writing it to disk in one go, it should come as no surprise that attempting to completely load it in memory fails for the same reason.

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

    Default Re: Issue with download from url

    Quote Originally Posted by ejoshva View Post
    Now I am able to download and write all the content using the readyRead()
    Qt Code:
    1. m_reply = m_manager->get(m_request);
    2.  
    3. connect(m_reply,SIGNAL(readyRead()),this,SLOT(readingReadyBytes()));
    4.  
    5.  
    6. void zoushoFtpTransfer::readingReadyBytes()
    7. {
    8. QFile file(m_localPath);
    9. file.open(QIODevice::writeOnly);
    10. file.write(m_reply->read(m_reply->bytesAvailable()));
    11. file.close();
    12. qDebug()<<"size : "<<file.size();
    13. m_isDownloadSccess = true;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Again there is one more issue, after writing onto file, I am storing all the data for that file in QByteArray.
    Its storing in QBytearray till 134459022 of data, after which bad_alloc error is thrown.

    Let me know what data type can be used to store all the 318459022 size of data
    I'll give you a hint:

    7 = 4+3 // to read 7MB, read 4MB and then 3
    10 = 4+4+2 // to read 10MB read 4MB, then again 4MB and then 2 MB
    445 = 4+...+4+1 // to read 445MB read 4MB as long as there is more than 4MB to read and then read whatever remains

    And another hint:

    If you tell read() to read 4MB but there is only 1MB available, it will read 1MB

    Question:

    How much memory is needed to read 445MB of data this way?

    Now go and carve an algorithm out of it.
    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.


  18. The following user says thank you to wysota for this useful post:

    ejoshva (20th April 2015)

  19. #16
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Issue with download from url

    Thanks for your replies.

    @wysota - will try out an algorithm based on your hints

    Quote Originally Posted by wysota View Post
    I'll give you a hint:

    7 = 4+3 // to read 7MB, read 4MB and then 3
    10 = 4+4+2 // to read 10MB read 4MB, then again 4MB and then 2 MB
    445 = 4+...+4+1 // to read 445MB read 4MB as long as there is more than 4MB to read and then read whatever remains

    And another hint:

    If you tell read() to read 4MB but there is only 1MB available, it will read 1MB

    Question:

    How much memory is needed to read 445MB of data this way?

    Now go and carve an algorithm out of it.
    ans : 4 MB

Similar Threads

  1. How to download the source in the URL
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2012, 10:29
  2. From where should I download QML
    By prajnaranjan.das in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2011, 20:54
  3. where download kd chart2.2
    By yunpeng880 in forum General Discussion
    Replies: 3
    Last Post: 13th January 2009, 09:45
  4. Download Manager Like IDM(Internet Download Manager)
    By sathiskumarmsk in forum Qt Programming
    Replies: 1
    Last Post: 27th July 2008, 16:52
  5. i need to download qt-vsintegration
    By mmg123 in forum Installation and Deployment
    Replies: 4
    Last Post: 15th October 2006, 15:20

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.