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. How To Read a Text File Contents Located on a FTP Server
Forum Updated to NodeBB v4.3 + New Features

How To Read a Text File Contents Located on a FTP Server

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 16.2k Views 2 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.
  • R rezaMSLM

    @jsulm
    how to specify download directory

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #11

    @rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    R 1 Reply Last reply
    2
    • jsulmJ jsulm

      @rezaMSLM It is up to you where to store the file. You can use http://doc.qt.io/qt-5/qstandardpaths.html#locate to get a directory (for example the temp directory).

      R Offline
      R Offline
      rezaMSLM
      wrote on last edited by rezaMSLM
      #12

      @jsulm
      I want a sample Code not an english Link
      I understand C++ much better than English

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rezaMSLM
        wrote on last edited by rezaMSLM
        #13

        Where i am wrong?

        MainWindow::MainWindow(...)
        {
        my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt
        QTemporaryFile(QDir::tempPath());//store that file in temp path
        .
        .
        }
        
        void MainWindow::on_pushButton_Download_clicked()
        {
            ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
        }
        

        I Receive no error but no file is downloaded to temp folder

        jsulmJ 1 Reply Last reply
        0
        • R rezaMSLM

          Where i am wrong?

          MainWindow::MainWindow(...)
          {
          my_file=new QFile("Qt_Test.txt");//create new file named Qt_Test.txt
          QTemporaryFile(QDir::tempPath());//store that file in temp path
          .
          .
          }
          
          void MainWindow::on_pushButton_Download_clicked()
          {
              ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
          }
          

          I Receive no error but no file is downloaded to temp folder

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #14

          @rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

          MainWindow::MainWindow(...)
          {
          }
          
          void MainWindow::on_pushButton_Download_clicked()
          {
              QString path = QDir::tempPath() + '/' + "Qt_Test.txt";
              my_file=new QFile(path);
              my_file->open(QIODevice::WriteOnly);
              ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
          }

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply
          2
          • jsulmJ jsulm

            @rezaMSLM Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

            MainWindow::MainWindow(...)
            {
            }
            
            void MainWindow::on_pushButton_Download_clicked()
            {
                QString path = QDir::tempPath() + '/' + "Qt_Test.txt";
                my_file=new QFile(path);
                my_file->open(QIODevice::WriteOnly);
                ftp->get("Qt_Test.txt",my_file,QFtp::Ascii);//download Qt_Test.txt which is located on server 
            }
            R Offline
            R Offline
            rezaMSLM
            wrote on last edited by rezaMSLM
            #15

            @jsulm

            @jsulm said in How To Read a Text File Contents Located on a FTP Server:

            Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

            yes

            the file is created but is empty while the program is open!

            JonBJ 1 Reply Last reply
            0
            • R rezaMSLM

              @jsulm

              @jsulm said in How To Read a Text File Contents Located on a FTP Server:

              Did you check the example? http://doc.qt.io/archives/qt-4.8/qt-network-qftp-example.html

              yes

              the file is created but is empty while the program is open!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #16

              @rezaMSLM
              I could be wrong, but in @jsulm's code immediately after the ftp->get() line I would add line:

              my_file->close()
              

              I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....

              EDIT My assumption here was wrong, see further post below.

              R 1 Reply Last reply
              0
              • JonBJ JonB

                @rezaMSLM
                I could be wrong, but in @jsulm's code immediately after the ftp->get() line I would add line:

                my_file->close()
                

                I see that the example in the docs does not do that. Which seems a bit surprising, but the docs may know better than I do. However, assuming the file close is needed, you would get exactly the behaviour you describe (empty till exit program) if you do not close it. So I would give it a try and see....

                EDIT My assumption here was wrong, see further post below.

                R Offline
                R Offline
                rezaMSLM
                wrote on last edited by rezaMSLM
                #17

                @JonB
                I tried it but didn"t worked

                JonBJ 1 Reply Last reply
                0
                • R rezaMSLM

                  @JonB
                  I tried it but didn"t worked

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #18

                  @rezaMSLM
                  Hang on, I've just read more in the docs, as follows:

                  On the other hand, if you only want to work with the complete data, you can connect to the commandFinished() signal and read the data when the get() command is finished.

                  The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().

                  When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

                  So if I understand right, the get() merely initiates the transfer. I believe you need to write a commandFinished slot, and close the file there.

                  Ah yes, the example page goes on to show that. The example code has:

                      if (ftp->currentCommand() == QFtp::Get) {
                          if (error) {
                              statusLabel->setText(tr("Canceled download of %1.")
                                                   .arg(file->fileName()));
                              file->close();
                              file->remove();
                          } else {
                              statusLabel->setText(tr("Downloaded %1 to current directory.")
                                                   .arg(file->fileName()));
                              file->close();
                          }
                  

                  When a Get command is finished, a file has finished downloading (or an error occurred during the download).

                  I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents.

                  I think you have to follow the whole example code, or perhaps download the source files from there and p,lay with the whole project if you want to see how to use it fully.

                  R 1 Reply Last reply
                  0
                  • hskoglundH Offline
                    hskoglundH Offline
                    hskoglund
                    wrote on last edited by
                    #19

                    Hi, also you could check that the FTP username and password are correct.

                    JonBJ 1 Reply Last reply
                    0
                    • hskoglundH hskoglund

                      Hi, also you could check that the FTP username and password are correct.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #20

                      @hskoglund
                      Yes, but isn't username/password checking performed/errored much earlier than when he gets to issuing the get()? It's performed during FTP connection open.

                      The behaviour he describes --- file content not "visible" till exit application, then correct --- is explained exactly by failure to close output file, at least under Windows, which has a habit of marking files to be 0 length till closed by writer.

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @rezaMSLM
                        Hang on, I've just read more in the docs, as follows:

                        On the other hand, if you only want to work with the complete data, you can connect to the commandFinished() signal and read the data when the get() command is finished.

                        The function does not block and returns immediately. The command is scheduled, and its execution is performed asynchronously. The function returns a unique identifier which is passed by commandStarted() and commandFinished().

                        When the command is started the commandStarted() signal is emitted. When it is finished the commandFinished() signal is emitted.

                        So if I understand right, the get() merely initiates the transfer. I believe you need to write a commandFinished slot, and close the file there.

                        Ah yes, the example page goes on to show that. The example code has:

                            if (ftp->currentCommand() == QFtp::Get) {
                                if (error) {
                                    statusLabel->setText(tr("Canceled download of %1.")
                                                         .arg(file->fileName()));
                                    file->close();
                                    file->remove();
                                } else {
                                    statusLabel->setText(tr("Downloaded %1 to current directory.")
                                                         .arg(file->fileName()));
                                    file->close();
                                }
                        

                        When a Get command is finished, a file has finished downloading (or an error occurred during the download).

                        I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents.

                        I think you have to follow the whole example code, or perhaps download the source files from there and p,lay with the whole project if you want to see how to use it fully.

                        R Offline
                        R Offline
                        rezaMSLM
                        wrote on last edited by
                        #21

                        @JonB said in How To Read a Text File Contents Located on a FTP Server:

                        I do believe the reason your file is empty is that it must be closed, at the correct time, before you will see contents

                        you were right!
                        this code solved the problem

                        void MainWindow::ftpCommandFinished(int id, bool error)
                        {
                        .
                        .
                            if(my_file->isOpen())
                            {
                                 my_file->close();
                            }
                        .
                        .
                        }
                        
                        1 Reply Last reply
                        1
                        • Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by
                          #22

                          @rezaMSLM if your issue is solved please don't forget to mark your post as such. Thank you

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          2

                          • Login

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