[solved]Can Qt download a setup say .msi/.exe files?



  • Hi friends,
    Can I use Qt to download a setup file say .exe or .msi? If yes share some pointer for me. I tried given download manager example but it is currupting the setup file.



  • Just use QNetworkAccessManager to download the file, which can be - of course - used to download .exe and .msi as well (it is just a byte stream after all).

    If downloaded executable files are corrupted my first bet would be a virus scanner / malware detection tool.

    But feel free to share some code which reproduces your problem so we can talk about.



  • [quote author="mady" date="1319802429"]Hi friends,
    Can I use Qt to download a setup file say .exe or .msi?[/quote]
    Yes.
    [quote]If yes share some pointer for me.[/quote]
    0x01EF3BA0

    Sorry, I could not help myself, it is friday after all.
    !http://imgs.xkcd.com/comics/pointers.png(Pointers)!
    (From: xkcd.com)
    [quote]
    I tried given download manager example but it is currupting the setup file.
    [/quote]
    Then I guess you are writing it away in the wrong way, perhaps you are trying to write away binary data as a text file?



  • :)...
    Andre, I think your guess is correct but I don't know any other way to do it. Below is the code I am using to save the file into disk.
    @
    saveToDisk(const QString &filename, QIODevice *data)
    {
    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly)) {
    fprintf(stderr, "Could not open %s for writing: %s\n",
    qPrintable(filename),
    qPrintable(file.errorString()));
    return false;
    }

     file.write(data->readAll());
     file.close();
    

    }
    @


  • Moderators

    Is all the data available at the time you call data->readAll()? QIODevice is asynchronous, so you need to wait for all the data or you loop, writing away the data as it arrives. The latter will use up way less of your valuable RAM and will not crash if the data file is bigger than the available memory:-)



  • I am calling savetodisk() from my finished SLOT with the assumption that reply should have all the data. Please correct me if I am wrong.

    BTW, how to ensure if reply has complete data or not? How to loop or wait for complete date. Please consider me as a beginner.



  • After some experiments I can see call is coming to downloadProgress slot but there available byte (i.e. 2nd argument) is not correct. It seems somehow we are not able to read whole bytes. I tried to change the reply buffer size e.g. @currentDownload->setReadBufferSize(1024);@ but behavior is same. Any idea where problem could be?
    Since I am not reading actual size of bytes I am not considering writing in text formate is causing the issue.

    Note:

    1. I am behind proxy for that I am using @QNetworkProxyFactory::setUseSystemConfiguration ( true) ;@. Can it cause the issue?
    2. My actual file size is 5.81 MB, can it be an issue?


  • The assumption that the reply will have all bytes is wrong. What you should do, is connect to the readyRead slot of your data device, and from that slot in a loop read all the then available data like so:

    @
    while (data.bytesAvailable() > 0) {
    file.write(data.readAll());
    }
    @

    That will ensure that also data that arrives in the time that you are writing to the file will be read properly. Note that the signal will probably fire multiple times. Note that this is desireable behaviour! For one, it makes it possible for you to easily keep your GUI responsive while waiting for the (slow!) network or file data to come in.

    You know that you have all data, when you get the finished() signal from the QNetworkReply.



  • Hey Ander,
    thanks for your reply but it didn't solve the problem. I tried following code in readReady slot
    @
    void Myclass::MyReadyRead()
    {
    while(myClassObj->bytesAvailable()>0)
    {
    output.write(myClassObj->readAll());
    }
    }
    @
    I am not seeing multiple calls even. Before coming to readReady() it is going into DownloadProgress slot where I can see only 220byte are available and received. It seems some how my program is not able to read complete data.

    As I menied previously I am behind proxy can it cause this issue? Asking this because without that proxy fix i.e. "QNetworkProxyFactory::setUseSystemConfiguration ( true)" it was going into trans state.

    Note: I am able to download setup through broswer.



  • So... what does this DownloadProgress slot look like?



  • I am not doing anything inside DownloadProgress() slot, do I need to do anything? I am Just checking both arguments, first argument is telling howmany bytes received and second one is saying how my total bytes available. In this case I am getting both as 220 bytes whereas my file size is almost 5.81MB. Not sure what is going wrong here.
    I successfully download one .pdf file which was almost 1MB. I am not sure if .msi file format is causing any problem here.



  • I am not sure if I am looking the problem from correct direction but for .msi files or any executable files it gives options to select if user wanted to save/run/cancel then it will actually go and download the file.
    So do I need to first detect the option clicked by use then download the file? If yes could you please let me know how to do that?



  • [quote author="mady" date="1319880414"]I am not doing anything inside DownloadProgress() slot, do I need to do anything? I am Just checking both arguments, first argument is telling howmany bytes received and second one is saying how my total bytes available. In this case I am getting both as 220 bytes whereas my file size is almost 5.81MB. Not sure what is going wrong here. [/quote]
    No, you do not need to do anything, but it might have been that you did something that blocks the further receiving of more bytes.

    [quote author="mady" date="1320047021"]I am not sure if I am looking the problem from correct direction but for .msi files or any executable files it gives options to select if user wanted to save/run/cancel then it will actually go and download the file.[/quote]
    "It"? What gives options exactly? Your application, or a normal browser application?

    [quote]So do I need to first detect the option clicked by use then download the file? If yes could you please let me know how to do that? [/quote]
    What kind of application are you trying to build, exactly? Now it starts to sound like some kind of webbrowser... Could you be a bit more clear on what Qt classes you are currently using?



  • Thanks every one, I managed to resolve the issue. Issue I was facing due to http redirection.

    Thanks a lot once again for your support.



  • Just as a side note for the archive:
    In those cases, it's often useful to test the operation with a command line utility like "curl":http://curl.haxx.se/. Calling it with some verbosity switch you can see what's going on, including redirects, the complete HTTP headers, etc. Browsers usually hide that stuff to you.


Log in to reply
 

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