Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can't get file to download



  • I''m trying to download video from an url

    QNetworkAccessManager manager;
    QUrl videoUrl = "https://www.animeunityserver15.cloud/DDL/Anime/SewayakiKitsuneNoSenko-san/SewayakiKitsuneNoSenko-san_Ep_01_SUB_ITA.mp4";
    QNetworkReply* response = manager.get(QNetworkRequest(videoUrl));
    QFile file("./video.mp4");
    file.open(QIODevice::WriteOnly);
    file.write(response->readAll());
    file.close();
    

    I don't get errors but the file is 0kb, meaning it's probably not getting downloaded.
    What am I doing wrong?



  • I don't believe you are using this correctly. Remember that the framework is event driven, meaning that when you send your request you don't want to stall until a response comes in. Read the docs for QNetworkAccessManager and use the signals/slots correctly to read the data (when it arrives). Also, the docs example looks like you may have to set your own user agent header for it to work. Finally, are you sure your Qt framework is setup with openSSL to handle secure https connections?



  • Hi, indeed as @Kent-Dorfman says, you need an event-driven approach, something like this:

    QNetworkAccessManager* manager = new QNetworkAccessManager(this);
    QUrl videoUrl("https://www.animeunityserver15.cloud/DDL/Anime/SewayakiKitsuneNoSenko-san/SewayakiKitsuneNoSenko-san_Ep_01_SUB_ITA.mp4");
    manager->get(QNetworkRequest(videoUrl));
    connect(manager,&QNetworkAccessManager::finished,[] (QNetworkReply* response)
    {
        QFile file("./video.mp4");
        file.open(QIODevice::WriteOnly);
        file.write(response->readAll());
        file.close();
    });

  • Moderators

    @federicomorrone said in Can't get file to download:

    I don't get errors but the file is 0kb, meaning it's probably not getting downloaded.
    What am I doing wrong?

    You must wait for the download to finish before you call response->readAll().

    Listen for the QNetworkReply::finished() signal.



  • @jksh said in Can't get file to download:

    You must wait for the download to finish before you call response->readAll().
    Listen for the QNetworkReply::finished() signal.

    Just to be nitpicky...would the "more correct" solution be to write the data as it comes in based on readyRead and use finish() to close the file? This is for the 99.9999999th% case where the file is bigger than available memory for queuing. It's one of those "probably never cause a problem in my lifetime" scenarios, but should maybe be considered?


  • Moderators

    @kent-dorfman said in Can't get file to download:

    @jksh said in Can't get file to download:

    You must wait for the download to finish before you call response->readAll().
    Listen for the QNetworkReply::finished() signal.

    Just to be nitpicky...would the "more correct" solution be to write the data as it comes in based on readyRead and use finish() to close the file? This is for the 99.9999999th% case where the file is bigger than available memory for queuing. It's one of those "probably never cause a problem in my lifetime" scenarios, but should maybe be considered?

    Yes, that is indeed the more robust solution.

    So @FedericoMorrone, you can get your code to work by using the finished() signal. However, if you need to download very large files, you should use the readyRead() signal and write data into your file in chunks.


Log in to reply