Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't get file to download
Qt 6.11 is out! See what's new in the release blog

Can't get file to download

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 696 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FedericoMorrone
    wrote on last edited by
    #1

    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?

    JKSHJ 1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      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?

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      1 Reply Last reply
      4
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        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();
        });
        1 Reply Last reply
        6
        • F FedericoMorrone

          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?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @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.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          4
          • Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by Kent-Dorfman
            #5

            @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?

            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

            JKSHJ 1 Reply Last reply
            5
            • Kent-DorfmanK Kent-Dorfman

              @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?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @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.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved