Results 1 to 2 of 2

Thread: Issue with download and writing of file using ready read

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

    Default Re: Issue with download and writing of file using ready read

    I am trying to download a file from server and write onto a file using readyRead() of QNetworkReply .
    Since I am facing problem in keeping the whole file say 600Mb in buffer and writing in one go as Windows doesn't allow buffer data more than 150Mb.

    Qt Code:
    1. bookdownloader::bookdownloader(QUrl url, QObject *parent,QString zipFilePath) : QObject(parent)
    2. {
    3. qb.setWindowTitle("Download in Progress");
    4. progressDialog = new QProgressDialog("Preparing to Download", "Cancel", 0, 100);
    5. progressDialog->setWindowTitle("Downloading");
    6. progressDialog->show();
    7. QNetworkAccessManager *m_WebCtrl = new QNetworkAccessManager(this);
    8.  
    9. setFilePath(zipFilePath);
    10. file.setFileName(this->getFilePath());
    11.  
    12. downloadedDataSize = 0;
    13. QByteArray postData;
    14.  
    15. QNetworkRequest request(url);
    16. request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    17.  
    18. reply = m_WebCtrl->get(request);
    19.  
    20. connect(reply,SIGNAL(readyRead()),this,SLOT(slotReadData()));
    21. connect(reply,SIGNAL(finished()), this,SLOT(fileDownloaded()));
    22. connect(reply, SIGNAL(downloadProgress(qint64,qint64)),this, SLOT(downloadprogress(qint64,qint64)));
    23. }
    24.  
    25. void bookdownloader::fileDownloaded()
    26. {
    27. progressDialog->close();
    28. emit downloaded();
    29. }
    30.  
    31. void bookdownloader::downloadprogress(qint64 recieved, qint64 total)
    32. {
    33. if(total < 1)
    34. return;
    35.  
    36. progressDialog->setValue((recieved*100/total));
    37.  
    38. if (progressDialog->wasCanceled())
    39. {
    40. progressDialog->close();
    41. m_WebCtrl->blockSignals(true);
    42. }
    43.  
    44. progressDialog->setLabelText(QString::number((recieved*100/total))+"%");
    45.  
    46. }
    47.  
    48. void bookdownloader::slotReadData()
    49. {
    50. file.setFileName(this->getFilePath());
    51. if(!file.open(QIODevice::WriteOnly| QIODevice::Append))
    52. {
    53. QMessageBox::critical(NULL,"Hello!","Error saving file!");
    54. return;
    55. }
    56. QDataStream out(&file);
    57. out << reply->readAll();
    58. file.close();
    59.  
    60. downloadedDataSize += file.size();
    61. }
    To copy to clipboard, switch view to plain text mode 

    Once the download and writing onto file is complete, when I try to manually unzip the file error thrown as "file is corrupt"

    In the extracter code when I try to find the list in the zip file, it's 0.

    Kindly let me know what's going wrong in the download and writing onto file part by part.


    Added after 40 minutes:


    Got it working with the code below

    Qt Code:
    1. void bookdownloader::slotReadData()
    2. {
    3. file.setFileName(this->getFilePath());
    4. file.open(QIODevice::Append);
    5. file.write(reply->read(reply->bytesAvailable()));
    6. file.close();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ejoshva; 8th August 2015 at 06:40.

  2. #2
    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: Issue with download and writing of file using ready read

    You do not want to be using QDataStream to write the raw binary information into your file. You are giving the QDataStream a series of QByteArrays, and it serialises those as a size value (4 bytes IIRC) before the raw data it contains. Your resulting file will be larger than the source file with "random" bytes inserted between the blocks of bytes you actually wanted.

    Simply call file->write() with the QByteArray you get from readAll().

    As an aside:
    • You are appending to a file that this code does not ensure is empty.
    • Repeatedly opening and closing the file takes time and does not achieve a great deal. Open it once when you start the transfer and close it when the transfer finishes.
    • The transfer can finish because of an error you do not check for. What should happen to the target file in that circumstance?

Similar Threads

  1. Issue with download from url
    By ejoshva in forum Newbie
    Replies: 15
    Last Post: 20th April 2015, 13:29
  2. file not writing
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2011, 23:01
  3. Read contents from the file using QHTTP Read()?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 21st June 2011, 08:03
  4. XML file writing
    By mbjerkne in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2006, 19:04
  5. Writing an XML file
    By qball2k5 in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2006, 10:58

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.