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. Some way to get the file name with url?
Forum Updated to NodeBB v4.3 + New Features

Some way to get the file name with url?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 6.7k 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.
  • J Offline
    J Offline
    Jeronimo
    wrote on 20 Oct 2016, 22:53 last edited by
    #1

    Hi i am doing one http client but when i save the file i need to put the same extension so if i download one txt must be txt, php must php etc..
    But i can get since the url for example: google.com/file.php <-- this file because i was searching and i checked something that maybe works:

    QFileInfo fileInfo(url.path());
    QString fileName=fileInfo.fileName();
    

    Some way to do this. Thx in advance.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Paul Colby
      wrote on 20 Oct 2016, 23:20 last edited by
      #2

      Hi @Jeronimo

      No need to use QFileInfo - just use QUrl::fileName() directly :)

      qDebug() << QUrl("http://google.com/file.php").fileName(); // "file.php"
      

      Note, depending on the use case, you might also want to look at the Content-Disposition header (if any) included in the HTTP server's response too.

      Cheers.

      J 1 Reply Last reply 20 Oct 2016, 23:35
      2
      • P Paul Colby
        20 Oct 2016, 23:20

        Hi @Jeronimo

        No need to use QFileInfo - just use QUrl::fileName() directly :)

        qDebug() << QUrl("http://google.com/file.php").fileName(); // "file.php"
        

        Note, depending on the use case, you might also want to look at the Content-Disposition header (if any) included in the HTTP server's response too.

        Cheers.

        J Offline
        J Offline
        Jeronimo
        wrote on 20 Oct 2016, 23:35 last edited by Jeronimo
        #3

        @Paul-Colby said in Some way to get the file name with url?:

        QUrl("http://google.com/file.php").fileName();

        Ok thx one question about one issue that i have about this. I call since my main function and when i call the method i pass the string. But i dont know how i can use for example since my main function to say one string (the url of my website) and this will be used in my slot of my other class.

        Main cpp:

        #include <QCoreApplication>
        #include "downloader.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            Downloader d;
            d.doDownload("http://mywebsite/currxml.php");
        
            return a.exec();
        }
        

        Downloader.h:

        #ifndef DOWNLOADER_H
        #define DOWNLOADER_H
        
        #include <QObject>
        #include <QNetworkAccessManager>
        #include <QNetworkRequest>
        #include <QNetworkReply>
        #include <QUrl>
        #include <QDateTime>
        #include <QFile>
        #include <QDebug>
        
        class Downloader : public QObject
        {
            Q_OBJECT
        public:
            explicit Downloader(QObject *parent = 0);
        
            void doDownload(QString s);
        
        signals:
        
        public slots:
            void replyFinished (QNetworkReply *reply);
        
        private:
           QNetworkAccessManager *manager;
        
        };
        
        #endif // DOWNLOADER_H
        

        Downloader.cpp:

        #include "downloader.h"
        
        Downloader::Downloader(QObject *parent) :
            QObject(parent)
        {
        }
        
        void Downloader::doDownload(QString s)
        {
            manager = new QNetworkAccessManager(this);
        
            connect(manager, SIGNAL(finished(QNetworkReply*)),
                    this, SLOT(replyFinished(QNetworkReply*)));
        
            manager->get(QNetworkRequest(QUrl(s)));
        }
        
        void Downloader::replyFinished (QNetworkReply *reply)
        {
            if(reply->error())
            {
                qDebug() << "ERROR!";
                qDebug() << reply->errorString();
            }
            else
            {
                qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
                qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
                qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
                qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
                qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
                QString p =QUrl("http://myweb/currxml.php").fileName();
                QFile *file = new QFile("C:/Users/moh/"+p);
                if(file->open(QFile::Append))
                {
                    file->write(reply->readAll());
                    file->flush();
                    file->close();
                }
                delete file;
            }
        
            reply->deleteLater();
        }
        

        If you have some suggestion will help me a lot sorry again!

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Paul Colby
          wrote on 21 Oct 2016, 00:41 last edited by
          #4

          Instead of

          QString p =QUrl("http://myweb/currxml.php").fileName();
          

          try

          QString p = reply->request().url().fileName();
          

          Cheers.

          1 Reply Last reply
          4

          1/4

          20 Oct 2016, 22:53

          • Login

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