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

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

  • check QNetworkRequest and content-length

  • I made function:
    void MainWindow::fileSize(){
    QNetworkRequest req;
    QUrl url(UrlList.at(0));
    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.

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

  • I already done it, previous code was wrong I changed it to:
    void MainWindow::fileSize(){
    QNetworkRequest req;
    manager = new QNetworkAccessManager(this); //cannot be local var
    reply__ = manager->head(req);
    void MainWindow::num(){
    FileSize = reply__->header(QNetworkRequest::ContentLengthHeader).toUInt();

    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.

Log in to reply

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