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. [Solved]How can I get filesize of file at remote server?
Forum Updated to NodeBB v4.3 + New Features

[Solved]How can I get filesize of file at remote server?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.8k Views 1 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.
  • S Offline
    S Offline
    SetBetterPass
    wrote on 23 Feb 2013, 09:43 last edited by
    #1

    I have project in which I need to get file name and size of file at some remote server. I have URL for example http://www.example.com/picture.png. It's easy to get file name, I just pick last part of URL, but I have no idea how to get file size. Any ideas how can i get it?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bepehr
      wrote on 23 Feb 2013, 10:54 last edited by
      #2

      check QNetworkRequest and content-length

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SetBetterPass
        wrote on 23 Feb 2013, 12:36 last edited by
        #3

        I made function:
        @
        void MainWindow::fileSize(){
        QNetworkRequest req;
        QUrl url(UrlList.at(0));
        req.setHeader(QNetworkRequest::ContentLengthHeader);
        req.setUrl(url);
        QNetworkAccessManager manager;
        QNetworkReply* reply__ = manager.get(req);
        FileSize = reply__->header(QNetworkRequest::ContentLengthHeader).toUInt();
        }
        @
        but that doesn't work and I get error : no matching function for call to ‘QNetworkRequest::setHeader(QNetworkRequest::KnownHeaders)’
        candidates are: void QNetworkRequest::setHeader(QNetworkRequest::KnownHeaders, const QVariant&)

        what to put in as 'const QVariant&'? I tried to find some examples but only what I found were examples with POST where original data came from users computer but I need to get info about data at remote server...
        I also put there random number just to try and it run without error but I got no reply.

        1 Reply Last reply
        0
        • 1 Offline
          1 Offline
          10Htts
          wrote on 23 Feb 2013, 17:58 last edited by
          #4

          Here a way to get the file size:
          @reply = manager->get(*request);
          connect(reply,SIGNAL(downloadProgress(qint64,qint64)),SLOT(downloadProgress(qint64,qint64)));@
          The second qint64 is the file size.
          Hope it helps...

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SetBetterPass
            wrote on 23 Feb 2013, 18:58 last edited by
            #5

            I already done it, previous code was wrong I changed it to:
            @
            void MainWindow::fileSize(){
            FileSize=0;
            QNetworkRequest req;
            manager = new QNetworkAccessManager(this); //cannot be local var
            req.setUrl(QUrl(UrlList.at(0)));
            reply__ = manager->head(req);
            connect(reply__,SIGNAL(finished()),this,SLOT(num()));
            }
            void MainWindow::num(){
            FileSize = reply__->header(QNetworkRequest::ContentLengthHeader).toUInt();
            reply__->deleteLater();
            manager->deleteLater();
            }
            @

            Actually this works for me so giving it here if someone have this problem once :)
            And 10Htts I didn't want to download the file, only get info.

            1 Reply Last reply
            1

            1/5

            23 Feb 2013, 09:43

            • Login

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